213 lines
4.7 KiB
C++
213 lines
4.7 KiB
C++
// FTP.cpp : Implementation of CFTP
|
|
|
|
#include "stdafx.h"
|
|
#include "FTP.h"
|
|
|
|
#pragma comment(lib, "wininet.lib")
|
|
|
|
#define FTPAGENT "FACILITOR"
|
|
// CFTP
|
|
|
|
CFTP::~CFTP()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
STDMETHODIMP CFTP::InterfaceSupportsErrorInfo(REFIID riid)
|
|
{
|
|
static const IID* arr[] =
|
|
{
|
|
&IID_IFTP
|
|
};
|
|
|
|
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
|
|
{
|
|
if (InlineIsEqualGUID(*arr[i],riid))
|
|
return S_OK;
|
|
}
|
|
return S_FALSE;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::get_Proxyname(BSTR* pVal)
|
|
{
|
|
*pVal = m_Proxyname.AllocSysString();
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Proxyname(BSTR newVal)
|
|
{
|
|
m_Proxyname = CString(newVal);
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::get_Hostname(BSTR* pVal)
|
|
{
|
|
*pVal = m_Hostname.AllocSysString();
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Hostname(BSTR newVal)
|
|
{
|
|
m_Hostname = CString(newVal);
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::get_Port(LONG* pVal)
|
|
{
|
|
*pVal = m_Port;
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Port(LONG newVal)
|
|
{
|
|
m_Port = (INTERNET_PORT)newVal;
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::get_Username(BSTR* pVal)
|
|
{
|
|
*pVal = m_Username.AllocSysString();
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Username(BSTR newVal)
|
|
{
|
|
m_Username = CString(newVal);
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::get_Password(BSTR* pVal)
|
|
{
|
|
*pVal = m_Password.AllocSysString();
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Password(BSTR newVal)
|
|
{
|
|
m_Password = CString(newVal);
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::get_Flags(LONG* pVal)
|
|
{
|
|
*pVal = m_Flags;
|
|
|
|
return S_OK;
|
|
}
|
|
STDMETHODIMP CFTP::put_Flags(LONG newVal)
|
|
{
|
|
m_Flags = newVal;
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
CString inetError(CString method, CString func)
|
|
{
|
|
DWORD errcode = GetLastError();
|
|
CString err;
|
|
|
|
if (errcode != ERROR_INTERNET_EXTENDED_ERROR)
|
|
{
|
|
err.Format("%s\nCould not %s (%d): %s", method, func, errcode, myGetLastErrorMsg());
|
|
}
|
|
else
|
|
{
|
|
DWORD dwInetError;
|
|
DWORD dwExtLength = 8192;
|
|
TCHAR errmsg[8192];
|
|
int returned = InternetGetLastResponseInfo( &dwInetError, errmsg, &dwExtLength );
|
|
err.Format("%s\ndwInetError: (%d)\n%s", method, dwInetError, errmsg);
|
|
}
|
|
return err;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::Open()
|
|
{
|
|
if (m_hInternetConnect)
|
|
{
|
|
InternetCloseHandle(m_hInternetConnect);
|
|
m_hInternetConnect = NULL;
|
|
}
|
|
|
|
if (!m_hInternetOpen)
|
|
{
|
|
m_hInternetOpen = InternetOpen(FTPAGENT,
|
|
m_Proxyname==""?0:1, /*UseProxy*/
|
|
m_Proxyname, /*ProxyName*/
|
|
NULL, /*ProxyBypass*/
|
|
0); /*Flags*/
|
|
|
|
if (!m_hInternetOpen)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::Open", "InternetOpen"));
|
|
}
|
|
|
|
m_hInternetConnect = InternetConnect(m_hInternetOpen,
|
|
m_Hostname, m_Port,
|
|
m_Username, m_Password,
|
|
INTERNET_SERVICE_FTP, m_Flags, NULL);
|
|
if (!m_hInternetConnect)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::Open", "InternetConnect"));
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::SetCurrentDir(BSTR Directory)
|
|
{
|
|
if (!m_hInternetConnect)
|
|
return AtlReportError (GetObjectCLSID(), "\nCFTP::PutFSetCurrentDirileFromTextData open first");
|
|
|
|
BOOL success = FtpSetCurrentDirectory(m_hInternetConnect, CString(Directory));
|
|
if (!success)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::SetCurrentDir", "FtpSetCurrentDirectory"));
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::PutFile(BSTR remoteFileName, BSTR localFileName)
|
|
{
|
|
if (!m_hInternetConnect)
|
|
return AtlReportError (GetObjectCLSID(), "\nCFTP::PutFile open first");
|
|
|
|
BOOL success = FtpPutFile(m_hInternetConnect, CString(localFileName), CString(remoteFileName), FTP_TRANSFER_TYPE_BINARY, 0);
|
|
if (!success)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::PutFile", "FtpPutFile"));
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::PutFileFromTextData(BSTR remoteFileName, BSTR data)
|
|
{
|
|
if (!m_hInternetConnect)
|
|
return AtlReportError (GetObjectCLSID(), "\nCFTP::PutFileFromTextData open first");
|
|
|
|
CString sData(data);
|
|
HINTERNET hFile = FtpOpenFile(m_hInternetConnect, CString(remoteFileName),
|
|
GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0);
|
|
if (!hFile)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::PutFileFromTextData", "FtpOpenFile"));
|
|
|
|
DWORD written;
|
|
BOOL success = InternetWriteFile(hFile, sData, sData.GetLength(), &written);
|
|
if (!success)
|
|
return AtlReportError (GetObjectCLSID(), inetError("CFTP::PutFileFromTextData", "InternetWriteFile"));
|
|
|
|
//Log2File 1, "Transfer file size: " & Len(body) & " written: " & written
|
|
//SendFileFTP = True
|
|
InternetCloseHandle (hFile);
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CFTP::Close()
|
|
{
|
|
if (m_hInternetConnect)
|
|
InternetCloseHandle(m_hInternetConnect);
|
|
m_hInternetConnect = NULL;
|
|
|
|
if (!m_hInternetOpen)
|
|
InternetCloseHandle(m_hInternetOpen);
|
|
m_hInternetOpen = NULL;
|
|
|
|
return S_OK;
|
|
}
|