For detailed explanation of FtpGetFiles() function see official documentation

https://docs.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-ftpgetfilea

Searching this function online found that introduction to this function is a bit scarce, so I put my successful execution code here hoping to help people learning together.

  1. Implementation method:
  • Connect to server
  • Get ftp connection handle
  • Retrieve whether this file name exists in the project, if exists then download
  1. Use c++ to operate ftp according to documentation we should first create InternetOpen to get handle, and pass handle to InternetConnect function. This step is similar to connecting Mysql. Getting Connection handle allows us to use Ftp related functions
HINTERNET hint;
	HINTERNET hFtp;

	hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
	if (hint == NULL)
		return -1;
	hFtp = InternetConnect(hint, ftpSvrIp.c_str(), port, userName.c_str(), password.c_str(), INTERNET_SERVICE_FTP, 0, 0);
	if (hFtp == NULL)
		return -1;
  1. InternetConnect methods are below. Because referring to C++ Implement FTP Upload File Blog for automatic file download update, this article provides usage of file upload related functions. Image Description
  2. FtpGetFile function has a parameter <fFailIfExists> to control whether to overwrite local file with same name. Document notes, if set to True, function download fails after finding file with same name. If we hope to directly replace local file with same name when downloading, we can use False attribute. Since downloading files are mainly dll or xml file types, file type is set to FTP_TRANSFER_TYPE_UNKNOWN | INTERNET_FLAG_RELOAD, the latter is to prevent downloading old version file from cache when file updates.
  3. code
int main()
{
	char* buffer = _getcwd(NULL, 0);
	//ftp address
	string ftpSvrIp = "********";

	//ftp port
	int port = 21;

	//username
	string userName = "ftpUser";

	//password
	string password = "******";

	//upload source file path
	string sourceFilePath = "E:/11.txt";

	//upload destination file path
	string desFilePath = "./11.txt";

	vector<string> files;


	HINTERNET hint;
	HINTERNET hFtp;

	hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
	if (hint == NULL)
		return -1;
	hFtp = InternetConnect(hint, ftpSvrIp.c_str(), port, userName.c_str(), password.c_str(), INTERNET_SERVICE_FTP, 0, 0);
	if (hFtp == NULL)
		return -1;

	HINTERNET isHas = FtpFindFirstFile(hFtp, desFilePath.c_str(), NULL, 0, 0);

	if (isHas == NULL)
		return -1;
	// Using this method will not replace the file, but maintain the old version unchanged. Need to delete and re-download.
	// If you want to replace the original file when downloading, need to modify parameter <fFailFExits> to FALSE
	// Reason:
	// Indicates whether the function should proceed if a local file of the specified name already exists. If fFailIfExists is TRUE and the local file exists, FtpGetFile fails.
	// If this parameter is set to TRUE, automatically stop function execution fail after finding file with same name.
	BOOL bl = FtpGetFile(hFtp, desFilePath.c_str(), sourceFilePath.c_str(), FALSE, NULL, FTP_TRANSFER_TYPE_UNKNOWN | INTERNET_FLAG_RELOAD, 0);

	InternetCloseHandle(hint);
	InternetCloseHandle(hFtp);

	cout << "succeed" << endl;
	system("pause");

	return 0;
}