ftp/Win32.cpp

// Win32.cpp : Defines the entry point for the console application.
//

// Note for Non-Unicode Build:
// If you get one of the following errors add comsuppw.lib (Release) or comsuppwd.lib (Debug) to the linker's "Additional Dependencies".
// Win32.obj : error LNK2019: unresolved external symbol "wchar_t * __stdcall _com_util::ConvertStringToBSTR(char const *)"
// Win32.obj : error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)"

#include "stdafx.h"
#include "Win32.h"

// TODO: Insert your license key here.
TCHAR g_szLicenseKey[] =
_T("<?xml version=\"1.0\" ?>\r\n") \
_T("<License>\r\n") \
_T("<Version>2.0</Version>\r\n") \
_T("<Id>400012345</Id>\r\n") \
_T("<Name>name</Name>\r\n") \
_T("<Email>user@host.com</Email>\r\n") \
_T("<Company>company</Company>\r\n") \
_T("<Product>SmartFTP FTP Library</Product>\r\n") \
_T("<Features>\r\n") \
_T("<Feature>SFTP</Feature>\r\n") \
_T("</Features>\r\n") \
_T("<Users>1</Users>\r\n") \
_T("<Maintenance>2008-12-27</Maintenance>\r\n") \
_T("<Issue>2007-12-27</Issue>\r\n") \
_T("<Signature>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</Signature>\r\n") \
_T("</License>\r\n");

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // init COM
    ATLVERIFY(SUCCEEDED(::CoInitializeEx(NULL, COINIT_MULTITHREADED)));

    try
    {
        sfFTPLib::IGlobalPtr pGlobal;
        HRESULT hr = pGlobal.CreateInstance(__uuidof(sfFTPLib::Global));
        if(SUCCEEDED(hr))
        {          
            // verify license key
            if(pGlobal->LoadLicenseKeyData(g_szLicenseKey) == VARIANT_TRUE)
                _tprintf(_T("License key verified.\n"));
            else
                _tprintf(_T("Failed to verify license key.\n"));

            // UPnP
            sfFTPLib::IUPnPNATManagerPtr pUPnPNATManager;
            hr = pUPnPNATManager.CreateInstance(__uuidof(sfFTPLib::UPnPNATManager));
            if(SUCCEEDED(hr))
            {
                pUPnPNATManager->Initialize();
            }

            sfFTPLib::IFTPConnectionPtr ftp;
            hr = ftp.CreateInstance(__uuidof(sfFTPLib::FTPConnectionMTA));
            if(SUCCEEDED(hr))
            {
                ftp->LogFile = L"Win32Demo.log";

                // AUTH TLS
                ftp->Protocol = sfFTPLib::ftpProtocolSSLExplicit;
                ftp->Host = _T("smartftp.com");
                ftp->Port = 21;
                ftp->Username = _T("anonymous");
                ftp->Password = _T("bla@bla.com");
                ftp->Passive = VARIANT_TRUE;

                // No Proxy
                //ftp->Proxy->Type = sfFTPLib::ftpProxyTypeNone;
                //ftp->Proxy->Host =_T("192.168.1.10");
                //ftp->Proxy->Port = 1080;
                //ftp->Proxy->Authentication = VARIANT_TRUE;
                //ftp->Proxy->Username = _T("user");
                //ftp->Proxy->Password = _T("pass");    

                // log everything
                ftp->LogFile = _T("Win32Demo.log");

                // Connect
                sfFTPLib::enumError err = ftp->Connect();
                if(err == sfFTPLib::ftpErrorSuccess)
                {
                    // change directory
                    err = ftp->ChangeDirectory(_T("/SmartFTP"));
                    if(err == sfFTPLib::ftpErrorSuccess)
                    {
                        // read listing
                        err = ftp->ReadDirectory();
                        if(err == sfFTPLib::ftpErrorSuccess)
                        {
                            sfFTPLib::IFTPItemsPtr pDirectory = ftp->Items;
                            if(pDirectory)
                            {
                                int nCount = pDirectory->Count;
                                _tprintf(_T("Count = %d\n"), nCount);      

                                // Enum
                                if(nCount > 0)
                                {
                                    IEnumVARIANTPtr pEnum = pDirectory->_NewEnum;
                                    VARIANT *pArrVariant = new VARIANT[nCount];
                                    if(pArrVariant)
                                    {
                                        ULONG CeltFetched;
                                        if(SUCCEEDED(pEnum->Next(nCount, pArrVariant, &CeltFetched)))
                                        {
                                            for(ULONG i=0; i<CeltFetched; i++)
                                            {
                                                if(pArrVariant[i].vt == VT_DISPATCH)
                                                {
                                                    sfFTPLib::IFTPItemPtr pFTPItem = pArrVariant[i].pdispVal;
                                                    if(pFTPItem)
                                                    {                                  
                                                        _tprintf(_T("Type=0x%x; Name=%s; Size=%d\n"), pFTPItem->Type, (LPCTSTR)pFTPItem->Name, pFTPItem->Size);

                                                        if(pFTPItem->Type == sfFTPLib::ftpItemTypeLink)
                                                        {
                                                            _tprintf(_T("LinkPoint=%s"), (LPCTSTR)pFTPItem->LinkPoint);
                                                        }
                                                    }
                                                }
                                                ::VariantClear(&pArrVariant[i]);
                                            }
                                        }

                                        delete [] pArrVariant;
                                    }
                                }
                            }
                        }

                        // download file
                        err = ftp->DownloadFile(_T("History.txt"), _T("History.txt"), 0, 0);
                        if(err == sfFTPLib::ftpErrorSuccess)
                        {
                            _tprintf(_T("DownloadFile() successful.\n"));
                            _tprintf(_T("LastTransferBytes = %I64u B\n"), ftp->LastTransferBytes);
                            _tprintf(_T("LastTransferTime = %d s\n"), ftp->LastTransferTime);
                            _tprintf(_T("LastTransferSpeed = %d B/s\n"), ftp->LastTransferSpeed);
                        }
                        else
                        {
                            _tprintf(_T("DownloadFile() failed. Error=%d\n"), err);
                            _tprintf(_T("LastReplyCode = %d\n"), ftp->LastReplyCode);
                            _tprintf(_T("LastReply = %s\n"), (LPCTSTR)ftp->LastReply);
                        }
                   
                        // DownloadFileEx using IStream
                       
                        // create a storage
                        IStoragePtr pStorage;
                        if(SUCCEEDED(::StgCreateDocfile(_T("Storage"), STGM_NOSCRATCH | STGM_TRANSACTED | STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &pStorage)))
                        {
                            IStreamPtr pStream;
                            if(SUCCEEDED(pStorage->CreateStream(L"CONTENTS", STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pStream))
                                && pStream != NULL)
                            {
                                err = ftp->DownloadFileEx(_T("History.txt"), CComVariant(pStream.GetInterfacePtr()), 0, 0, 0, 0);
                                if(err == sfFTPLib::ftpErrorSuccess)
                                {
                                    _tprintf(_T("DownloadFileEx() successful.\n"));
                                }

                                pStream->Commit(STGC_OVERWRITE | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
                                pStorage->Commit(STGC_OVERWRITE | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE);
                            }
                        }
                    }
                }
                else
                {
                    if(err == sfFTPLib::ftpErrorLicense)
                        _tprintf(_T("Please acquire a license from http://www.smartftp.com\n"));
                    else
                        _tprintf(_T("Connect() failed. Error=%d\n"), err);
                }
            }
            else
            {
                _tprintf(_T("Failed to create CFTPConnection instance.\n"));
            }
       
            // UPnP
            if(pUPnPNATManager)
            {
                pUPnPNATManager->Uninitialize();
            }
        }
    }
    catch(_com_error &e)
    {
        _tprintf(_T("Com Error:\n"));
        _tprintf(_T("Code = %08lx\n"), e.Error());
        _tprintf(_T("Code meaning = %s\n"), (LPCTSTR) e.ErrorMessage());
        _tprintf(_T("Source = %s\n"), (LPCTSTR) e.Source());
        _tprintf(_T("Error Description = %s\n"), (LPCTSTR) e.Description());
    }

    ::CoUninitialize();

    return nRetCode;
}
 
© SmartSoft Ltd.