shellexecuteex失败(shellexecuteex失败 代码2)

一、FTP服务器

常用的FTP服务器有:Server-U,Filezilla Server,IIS。

Server-U的特点是功能强大,但是需要收费。

FileZilla Server是一种小巧、快速、可信赖的支持FTP以及SFTP的服务器端。它是开源的,并且具有很丰富的操作接口。

IIS是微软自带的FTP服务器,但是配置和操作非常的复杂。


Server-U


Filezilla Server

二、FTP客户端

常见FTP客户端工具:filezilla、LeapFTP、CuteFTP

filezilla

LeapFTP

CuteFTP

三、C++ FTP客户端操作框架

C++ FTP客户端框架:ftplibpp、ftplib、windows系统Wininet函数、libcurl、ftp.exe命令上传与下载文件。

ftplibpp, 提供ftp客户端功能的平台独立 C++ 库,支持Linux、Mac、window系统,支持 fxp, ssl/tl加密。https://github.com/mkulke/ftplibpp

ftplib, 提供ftp客户端功能的平台独立 C库,支持Linux (X86), Mac OS-X and OpenVMS (AXP)系统。http://nbpfaus.net/~pfau/ftplib/

windows系统Wininet函数,https://docs.microsoft.com/zh-cn/windows/win32/wininet/ftp-sessions

注意:windows中ftp.exe命令上传与下载文件方式比其他方式更加有效,其他方式不太稳定。

1、ftplibpp

函数说明:https://www.helplib.com/GitHub/article_110777

vs2015工程如何使用ftplib?

1)添加ftplib.h ftplib.cpp文件到工程中。

2)预处理器定义中添加NOSSL NOLFS _CRT_SECURE_NO_WARNINGS

3)ftplib.h头文件中增加

#include <winsock.h>#ifndef _WIN32#include <unistd.h>#include <sys/time.h>#else#include <winsock.h>#endif

2、ftplib

函数说明:http://nbpfaus.net/~pfau/ftplib/ftplib.html

3、windows系统Wininet函数

步骤:

1) InternetOpen初始化一个Internet句柄。此句柄用于建立一个FTP session。

2)InternetConnect创建一个FTP session。INTERNET_DEFAULT_FTP_PORT for the nServerPort parameter and INTERNET_SERVICE_FTP for the dwService parameter.

3)执行必要的操作。比如FtpPutFile、FtpGetFile、FtpDeleteFile、FtpRenameFile、FtpCreateDirectory、FtpRemoveDirectory、FtpGetCurrentDirectory、FtpSetCurrentDirectory等。

4)InternetCloseHandle关闭由InternetConnect创建的FTP session。

5)InternetCloseHandle关闭由InternetOpen创建的FTP session。

FtpCreateDirectory、FtpDeleteFile及之后的几个函数都需要InternetConnect返回的句柄。

常见函数介绍:

HINTERNET InternetOpen(

LPCTSTR lpszAgent,// 指定调用 WinINet 函数的应用程序或入口。该入口用作HTTP协议中用户代理项。其实是自定义的名称。如”MyFtp”、“mwj”等。

DWORD dwAccessType,//一般为INTERNET_OPEN_TYPE_PRECONFIG:返回注册表中的代理或直接的配置。

LPCTSTR lpszProxyName,//一般为NULL。若参数dwAccessType不是INTERNET_OPEN_TYPE_PROXY,此参数应被设为NULL。

LPCTSTR lpszProxyBypass,//一般为NULL。若参数dwAccessType不是INTERNET_OPEN_TYPE_PROXY,此参数应被设为NULL。

DWORD dwFlags);// INTERNET_FLAG_ASYNC:仅能用于作用在该函数返回的句柄的子句柄上的异步请求。INTERNET_FLAG_OFFLINE 与 INTERNET_FLAG_FROM_CACHE 相同:不做网络请求。所有的实体都由缓存返回。若请求条目不在缓存中,将返回一个错误。对于遍历FTP服务器上的文件夹时,此参数必须为0。

HINTERNET WINAPI InternetConnect(

HINTERNET hInternet, //InternetOpen返回的句柄

LPCTSTR lpszServerName, //要连接的Internet server的名字或IP

INTERNET_PORT nServerPort, //对FTP用INTERNET_DEFAULT_FTP_PORT

LPCTSTR lpszUserName, //对FTP可用“anonymous”。设为NULL,对FTP将自动设为anonymous

LPCTSTR lpszPassword, //若为NULL,对FTP则自动使用anonymous的默认密码

DWORD dwService, //对FTP用INTERNET_SERVICE_FTP

DWORD dwFlags, //一般为0

DWORD dwContext);//一般为0

此函数不仅可连接FTP还可连接HTTP。返回NULL表明连接失败。

FtpFindFirstFile和InternetFindNextFile遍历ftp文件

WIN32_FIND_DATA fd;HINTERNET hFind = FtpFindFirstFile(hFtpSession, "/*.*", &fd, INTERNET_FLAG_RELOAD, 0);if(hFind != INVALID_HANDLE_VALUE){ BOOL bFind = TRUE; while(bFind) { bFind = InternetFindNextFile(hFind, &fd); OutputDebugString(fd.cFileName); OutputDebugString("\n"); }}InternetCloseHandle(hFind);实例:#include <afxinet.h>void main(){ BOOL dRes,pRes; HINTERNET hInternet; HINTERNET hConnect; hInternet = InternetOpen("Test Sample", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE); if ( NULL == hInternet ) { printf("InternetOpen Error:%d\n", GetLastError() ); } hConnect = InternetConnect(hInternet, "127.0.0.1"/*FTP服务器地址*/, INTERNET_DEFAULT_FTP_PORT/*FTP端口号,此为默认值---21*/, "admin"/*用户名*/, "123456"/*密码*/, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT || INTERNET_FLAG_PASSIVE,0 ); if ( NULL == hInternet ) { printf( "InternetConnect Error:%d\n", GetLastError() ); InternetCloseHandle(hInternet); }dRes = FtpGetFile(hConnect, "./download/test.txt", "D:\\test.txt", FALSE, FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_UNKNOWN, 0); if ( dRes == 0 ) { printf( "FtpGetFile Error:\n", GetLastError() ); }else{ printf( "下载文件成功!\n" ); } pRes = FtpPutFile(hConnect,"D:\\test.txt","test.txt",FTP_TRANSFER_TYPE_ASCII,0); if(pRes==0) { printf("上传文件失败!\n"); }else{ printf("上传文件成功!\n"); } InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); if(dRes&&pRes) return true; else return false;

4、libcurl实现ftp客户端(上传、下载、进度、断点续传)

https://blog.csdn.net/wu110112/article/details/72898630

https://blog.csdn.net/u012234115/article/details/83869486

5、ftp.exe命令上传文件

bool FtpUploadFile(std::string strUuid,std::string strIp,int nPort,std::stringstrLoginUsername,std::string strLoginPassword,std::string strMainPath,std::stringstrSubPath,std::string strLocalFilePath,std::string strRomuteFileName,bool bIsBinary){std::string strCommandFile = strMainPath;strCommandFile += "//";strCommandFile += strUuid;strCommandFile += "-command.tmp";FILE * pCommandFile = fopen(strCommand.c_str(),"w+");std::string strFileName = "";char *pSrcFilePath = (char *)strLocalFilePath.c_str();char *pFindPos = strrchr(pSrcFilePath,'/');if(pFindPos == NULL){pFindPos = strrchr(pSrcFilePath,'\\');}if(pFindPos != NULL){strFileName = strLocalFilePath.substr((pFindPos-pSrcFilePath)+1,strLocalFilePath.size()-((pFindPos-pSrcFilePath)+1));}if(pCommandFile != NULL){fprintf(pCommandFile,"open %s %d\n",strIp.c_str(),nPort);fprintf(pCommandFile,"USER %s\n",strLoginUsername.c_str());fprintf(pCommandFile,"%s\n",strLoginPassword.c_str());//create directoryfprintf(pCommandFile,"mkdir %s\n",strMainPath.c_str());fprintf(pCommandFile,"cd %s\n",strMainPath.c_str());fprintf(pCommandFile,"mkdir %s\n",strSubPath.c_str());fprintf(pCommandFile,"cd %s\n",strSubPath.c_str());if(bIsBinary){fprintf(pCommandFile,"binary\n");}else{fprintf(pCommandFile,"ascii\n");}fprintf(pCommandFile,"prompt off\n");fprintf(pCommandFile,"delete %s\n",strRomuteFileName.c_str());fprintf(pCommandFile,"put %s\n",strLocalFilePath.c_str());//renameif(strRomuteFileName.size() > 0 && strFileName != strRomuteFileName){fprintf(pCommandFile,"rename %s %s\n",strFileName.c_str(),strRomuteFileName.c_str());}fprintf(pCommandFile,"quit\n");fclose(pCommandFile);std::string strParameter = "-n -s:" + strCommandFile;SHELLEXECUTEINFO shExecInfo = {0};shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);shExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;shExecInfo.hwnd = NULL;shExecInfo.lpVerb = NULL;shExecInfo.lpFile = "ftp.exe";shExecInfo.lpParameters = strParameter;shExecInfo.lpDirectory = NULL;shExecInfo.nShow = SW_HIDE;shExecInfo.hInstApp = NULL;ShellExecuteEx(&shExecInfo);WaitForSingleObject(shExecInfo.hProcess,INFINITE);DeleteFile(strCommandFile.c_str());return true;}else{return false;}}