Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

The Windows Internet Services (WinInet) functions offer three methods for retrieving files from an FTP server:

If the URL of the file is available, the application can call InternetOpenUrlto connect to that URL and then use InternetReadFileto control the download of the file. This setup allows the application tighter control over the download and is ideal for situations where no other operations need to be made on the FTP server. For more information about how to directly access resources, see Accessing URLs Directly.

If the application has established an FTP session handle to the server with InternetConnect, the application can call FtpOpenFilewith the existing file name and with a new name for the locally stored file. The application can then use InternetReadFileto download the file. This allows the application tighter control over the download and keeps the connection to the FTP server, so more commands can be executed.

If the application does not need tight control over the download, the application can use FtpGetFilewith the FTP session handle, remote file name, and local file name to retrieve the file. FtpGetFileperforms all the bookkeeping and overhead associated with reading a file from an FTP server and storing it locally.

The following example shows how to retrieve the file indicated by the IDC_FTPEdit2edit box and stores it locally using the file name specified by the IDC_FTPEdit3edit box. The HINTERNEThandle hSecondarywas created by InternetConnectafter establishing an FTP session. DisplayDiris another function that is designed to enumerate the directory.

Copy Code
int WINAPI GetFile(HWND hX)
{
	char strInFile[80];
	char strOutFile[80];
	int intTransType;
	strInFile[0]=0;
	strOutFile[0]=0;

	GetDlgItemText(hX,IDC_FTPEdit3,strOutFile,80);
	GetDlgItemText(hX,IDC_FTPEdit2,strInFile,80);

	if ((strlen(strOutFile)==0) || (strlen(strInFile)==0))
	{
		MessageBox(hX,"Target File or Destination File
Missing","Get File",MB_OK);
		return 0;
}
	else
	{
		intTransType = MessageBox(hX,
			"Do you want to download in ASCII (Default:Binary)?",
			"Get File",MB_YESNO);
		if (intTransType==IDYES)
		{
			if(!FtpGetFile(hSecondary,strInFile,strOutFile,FALSE,
				FILE_ATTRIBUTE_NORMAL,FTP_TRANSFER_TYPE_ASCII | 
				INTERNET_FLAG_NO_CACHE_WRITE,0))
			{
				ErrorOut(hX,GetLastError(),"Get File");
				DisplayDir(hX,INTERNET_FLAG_RELOAD);
				return 0;
		}
			else
			{
				MessageBox(hX,"ASCII Transfer Complete","Get
File",MB_OK);
				return 1;
		}
	}
		else
		{
			if(!FtpGetFile(hSecondary,strInFile,strOutFile,FALSE,
				FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY |
				INTERNET_FLAG_RELOAD,0))
			{
				ErrorOut(hX,GetLastError(),"Get File");
				return 0;
		}
			else
			{
				MessageBox(hX,"Binary Transfer Complete","Get
File",MB_OK);
				return 1;
		}
	}
}
}

See Also

Concepts

FTP Sessions