⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpcom.cpp

📁 一个FTP客户端COM组件的例子
💻 CPP
字号:
// ftpcom.cpp : Implementation of Cftpcom
#include "stdafx.h"
#include "Ftpserver.h"
#include "ftpcom.h"
#include <comdef.h>
#include <afxsock.h>
#include "winsock.h"
#include <afxinet.h>
#include <afxwin.h>

/////////////////////////////////////////////////////////////////////////////
// Cftpcom


STDMETHODIMP Cftpcom::get_URL(BSTR *pVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here

*pVal=_com_util::ConvertStringToBSTR(m_szUrl);
	return S_OK;
}

STDMETHODIMP Cftpcom::put_URL(BSTR newVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
strcpy(m_szUrl,_com_util::ConvertBSTRToString(newVal));
	return S_OK;
}

STDMETHODIMP Cftpcom::get_UserName(BSTR *pVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
*pVal=_com_util::ConvertStringToBSTR(m_szUserName);
	return S_OK;
}

STDMETHODIMP Cftpcom::put_UserName(BSTR newVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
strcpy(m_szUserName,_com_util::ConvertBSTRToString(newVal));
	return S_OK;
}

STDMETHODIMP Cftpcom::get_Password(BSTR *pVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
*pVal=_com_util::ConvertStringToBSTR(m_szPassword);
	return S_OK;
}

STDMETHODIMP Cftpcom::put_Password(BSTR newVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
strcpy(m_szPassword,_com_util::ConvertBSTRToString(newVal));
	return S_OK;
}

/*STDMETHODIMP Cftpcom::DownLoad(BSTR bsRemoteFile, BSTR bsLocalFile, BOOL *pRtnVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here

	return S_OK;
}*/




STDMETHODIMP Cftpcom::Disconnect()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
 if (m_pFtpConnection != NULL)
   	    m_pFtpConnection->Close(); 
    delete m_pFtpConnection;
    m_pFtpConnection = NULL;
	if(m_pSession!=NULL)
	{
		m_pSession->Close();
		delete m_pSession;
		m_pSession=NULL;
	}
	return S_OK;
}

STDMETHODIMP Cftpcom::Connect(VARIANT_BOOL *pRtnVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
if (m_pFtpConnection != NULL)
		m_pFtpConnection->Close();
	delete m_pFtpConnection;
	m_pFtpConnection = NULL;
	m_pSession=new CInternetSession;
    m_pFtpConnection=m_pSession->GetFtpConnection(m_szUrl,m_szUserName,m_szPassword, INTERNET_INVALID_PORT_NUMBER,false);
	if(m_pFtpConnection==NULL)
	{
		delete m_pSession;
		*pRtnVal=VARIANT_FALSE;
		return S_FALSE;
	}
	else
	{   
		*pRtnVal=VARIANT_TRUE;
		return S_OK;
	}
return S_OK;
}

STDMETHODIMP Cftpcom::UpLoad(BSTR bsLocalFile, BSTR bsRemoteFile, VARIANT_BOOL *pRtnVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
   CFile LocFile;
   CInternetFile* pSerFile;
	//CFileException ex;
	tempDir="";
	tempDir+="\\";
 	m_pFtpConnection->SetCurrentDirectory(tempDir);
	unsigned int nRead=0;
	DWORD LocFileLen=0;
	DWORD filelength;
	char* Locfilename;
	char* Remotefilename;
	char buf[1001];
	Locfilename=_com_util::ConvertBSTRToString(bsLocalFile);
	if(!LocFile.Open(Locfilename,CFile::modeRead|CFile::typeBinary|CFile::shareDenyNone))
	{  *pRtnVal=VARIANT_FALSE;	
		return S_FALSE;
	}
	
	 filelength=LocFile.GetLength();		
	Remotefilename=_com_util::ConvertBSTRToString(bsRemoteFile);
	pSerFile=m_pFtpConnection->OpenFile(Remotefilename,GENERIC_WRITE);
	do
	{
		nRead=LocFile.Read(buf,1000);
		if(nRead)
			pSerFile->Write(buf,nRead);
	}while(nRead==filelength);
	LocFile.Close();
	pSerFile->Close();
	delete pSerFile;
	pSerFile=NULL;

    *pRtnVal=VARIANT_TRUE;
	return S_OK;

}

STDMETHODIMP Cftpcom::get_Port(long *pVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
*pVal=m_lPort;
	return S_OK;
}

STDMETHODIMP Cftpcom::put_Port(long newVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
    m_lPort=newVal;
	return S_OK;
}

STDMETHODIMP Cftpcom::DelFile(BSTR bsRemoteFile, VARIANT_BOOL *pRtnVal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
    *pRtnVal=VARIANT_FALSE;
    int flag=0;
	tempDir="";
	tempDir+="\\";
	CFtpFileFind  fileFind(m_pFtpConnection);
	m_pFtpConnection->SetCurrentDirectory(tempDir);
	 flag=fileFind.FindFile("*.*");
	while(flag)
	{
	    flag=fileFind.FindNextFile();
		m_pFtpConnection->Remove(fileFind.GetFileName());
	}
	fileFind.Close();
    *pRtnVal=VARIANT_TRUE;
	return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -