连接的IP地址在此函数 InternetConnect
#include<windows.h>
#include<stdio.h>
#include <WinInet.h>
#pragma comment(lib, "WinInet.lib")
void Ftp_ShowError(char *lpszText)
{
char szErr[MAX_PATH] = { 0 };
wsprintfA(szErr, "%s Error[%d]\n", lpszText, GetLastError());
#ifdef _DEBUG
MessageBoxA(NULL, szErr, "ERROR", MB_OK);
#endif
}
BOOL Ftp_SaveToFile(char *pszFileName, BYTE *pData, DWORD dwDataSize)
{
HANDLE hFile = CreateFileA(pszFileName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_ARCHIVE, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
Ftp_ShowError("CreateFile");
return FALSE;
}
DWORD dwRet = 0;
WriteFile(hFile, pData, dwDataSize, &dwRet, NULL);
CloseHandle(hFile);
return TRUE;
}
BOOL Ftp_UrlCrack(char *pszUrl, char *pszScheme, char *pszHostName, char *pszUserName, char *pszPassword, char *pszUrlPath, char *pszExtraInfo, DWORD dwBufferSize)
{
BOOL bRet = FALSE;
URL_COMPONENTSA uc = { 0 };
RtlZeroMemory(&uc, sizeof(uc));
RtlZeroMemory(pszScheme, dwBufferSize);
RtlZeroMemory(pszHostName, dwBufferSize);
RtlZeroMemory(pszUserName, dwBufferSize);
RtlZeroMemory(pszPassword, dwBufferSize);
RtlZeroMemory(pszUrlPath, dwBufferSize);
RtlZeroMemory(pszExtraInfo, dwBufferSize);
uc.dwStructSize = sizeof(uc);
uc.dwSchemeLength = dwBufferSize - 1;
uc.dwHostNameLength = dwBufferSize - 1;
uc.dwUserNameLength = dwBufferSize - 1;
uc.dwPasswordLength = dwBufferSize - 1;
uc.dwUrlPathLength = dwBufferSize - 1;
uc.dwExtraInfoLength = dwBufferSize - 1;
uc.lpszScheme = pszScheme;
uc.lpszHostName = pszHostName;
uc.lpszUserName = pszUserName;
uc.lpszPassword = pszPassword;
uc.lpszUrlPath = pszUrlPath;
uc.lpszExtraInfo = pszExtraInfo;
bRet = InternetCrackUrlA(pszUrl, 0, 0, &uc);
if (FALSE == bRet)
{
Ftp_ShowError("InternetCrackUrl");
return bRet;
}
return bRet;
}
BOOL FTPDownload(char *pszDownloadUrl, BYTE **ppDownloadData, DWORD *pdwDownloadDataSize)
{
char szScheme[MAX_PATH] = { 0 };
char szHostName[MAX_PATH] = { 0 };
char szUserName[MAX_PATH] = { 0 };
char szPassword[MAX_PATH] = { 0 };
char szUrlPath[MAX_PATH] = { 0 };
char szExtraInfo[MAX_PATH] = { 0 };
RtlZeroMemory(szScheme, MAX_PATH);
RtlZeroMemory(szHostName, MAX_PATH);
RtlZeroMemory(szUserName, MAX_PATH);
RtlZeroMemory(szPassword, MAX_PATH);
RtlZeroMemory(szUrlPath, MAX_PATH);
RtlZeroMemory(szExtraInfo, MAX_PATH);
if (FALSE == Ftp_UrlCrack(pszDownloadUrl, szScheme, szHostName, szUserName, szPassword, szUrlPath, szExtraInfo, MAX_PATH))
{
return FALSE;
}
if (0 < lstrlenA(szExtraInfo))
{
lstrcatA(szUrlPath, szExtraInfo);
}
HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
HINTERNET hFTPFile = NULL;
BYTE *pDownloadData = NULL;
DWORD dwDownloadDataSize = 0;
DWORD dwBufferSize = 4096;
BYTE *pBuf = NULL;
DWORD dwBytesReturn = 0;
DWORD dwOffset = 0;
BOOL bRet = FALSE;
do
{
hInternet = InternetOpenA("WinInet Ftp Download V1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == hInternet)
{
Ftp_ShowError("InternetOpen");
break;
}
hConnect = InternetConnectA(hInternet, szHostName, INTERNET_INVALID_PORT_NUMBER, szUserName, szPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (NULL == hConnect)
{
Ftp_ShowError("InternetConnect");
break;
}
hFTPFile = FtpOpenFileA(hConnect, szUrlPath, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, 0);
if (NULL == hFTPFile)
{
Ftp_ShowError("FtpOpenFile");
break;;
}
dwDownloadDataSize = FtpGetFileSize(hFTPFile, NULL);
pDownloadData =(BYTE*)malloc(dwDownloadDataSize);
if (NULL == pDownloadData)
{
break;
}
RtlZeroMemory(pDownloadData, dwDownloadDataSize);
pBuf = (BYTE*)malloc(dwBufferSize);
if (NULL == pBuf)
{
break;
}
RtlZeroMemory(pBuf, dwBufferSize);
do
{
bRet = InternetReadFile(hFTPFile, pBuf, dwBufferSize, &dwBytesReturn);
if (FALSE == bRet)
{
Ftp_ShowError("InternetReadFile");
break;
}
RtlCopyMemory((pDownloadData + dwOffset), pBuf, dwBytesReturn);
dwOffset = dwOffset + dwBytesReturn;
} while (dwDownloadDataSize > dwOffset);
} while (FALSE);
if (FALSE == bRet)
{
free(pDownloadData);
pDownloadData = NULL;
dwDownloadDataSize = 0;
}
*ppDownloadData = pDownloadData;
*pdwDownloadDataSize = dwDownloadDataSize;
if (NULL != pBuf)
{
free(pBuf);
pBuf = NULL;
}
if (NULL != hFTPFile)
{
InternetCloseHandle(hFTPFile);
}
if (NULL != hConnect)
{
InternetCloseHandle(hConnect);
}
if (NULL != hInternet)
{
InternetCloseHandle(hInternet);
}
return bRet;
}
BOOL FTPUpload(char *pszUploadUrl, BYTE *pUploadData, DWORD dwUploadDataSize)
{
char szScheme[MAX_PATH] = { 0 };
char szHostName[MAX_PATH] = { 0 };
char szUserName[MAX_PATH] = { 0 };
char szPassword[MAX_PATH] = { 0 };
char szUrlPath[MAX_PATH] = { 0 };
char szExtraInfo[MAX_PATH] = { 0 };
RtlZeroMemory(szScheme, MAX_PATH);
RtlZeroMemory(szHostName, MAX_PATH);
RtlZeroMemory(szUserName, MAX_PATH);
RtlZeroMemory(szPassword, MAX_PATH);
RtlZeroMemory(szUrlPath, MAX_PATH);
RtlZeroMemory(szExtraInfo, MAX_PATH);
if (FALSE == Ftp_UrlCrack(pszUploadUrl, szScheme, szHostName, szUserName, szPassword, szUrlPath, szExtraInfo, MAX_PATH))
{
return FALSE;
}
if (0 < lstrlenA(szExtraInfo))
{
lstrcatA(szUrlPath, szExtraInfo);
}
HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
HINTERNET hFTPFile = NULL;
DWORD dwBytesReturn = 0;
BOOL bRet = FALSE;
do
{
hInternet = InternetOpenA("WinInet Ftp Upload V1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL == hInternet)
{
Ftp_ShowError("InternetOpen");
break;
}
hConnect = InternetConnectA(hInternet, szHostName, INTERNET_INVALID_PORT_NUMBER, szUserName, szPassword, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
if (NULL == hConnect)
{
Ftp_ShowError("InternetConnect");
break;
}
hFTPFile = FtpOpenFileA(hConnect, szUrlPath, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY | INTERNET_FLAG_RELOAD, 0);
if (NULL == hFTPFile)
{
Ftp_ShowError("FtpOpenFile");
break;;
}
bRet = InternetWriteFile(hFTPFile, pUploadData, dwUploadDataSize, &dwBytesReturn);
if (FALSE == bRet)
{
break;
}
} while (FALSE);
if (NULL != hFTPFile)
{
InternetCloseHandle(hFTPFile);
}
if (NULL != hConnect)
{
InternetCloseHandle(hConnect);
}
if (NULL != hInternet)
{
InternetCloseHandle(hInternet);
}
return bRet;
}
int main(int argc, char* argv[])
{
BYTE *pDownloadData = NULL;
DWORD dwDownloadDataSize = 0;
if (FALSE == FTPDownload("ftp://admin:123456789@192.168.0.1/Flower520.zip", &pDownloadData, &dwDownloadDataSize))
{
printf("FTP Download Error!\n");
}
Ftp_SaveToFile("myftpdownloadtest.zip", pDownloadData, dwDownloadDataSize);
free(pDownloadData);
pDownloadData = NULL;
printf("FTP Download OK.\n");
system("pause");
return 0;
}