方式1:
#include <windows.h>
#include <wininet.h>
#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:Entry ")
#pragma comment(lib, "wininet.lib ")
#pragma comment(lib, "kernel32.lib ")
void FileDownload()
{
HINTERNET hSession=InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
HINTERNET hConnection=InternetOpenUrl(hSession, "http://www.google.com ",NULL,0,0,0);
BYTE Buffer[4096];
DWORD Read=0,i;
HANDLE hFile=CreateFile( "c:\\index.htm ",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
InternetReadFile(hConnection,Buffer,sizeof(Buffer),&Read);
while(Read!=0)
{
WriteFile(hFile,Buffer,Read,&i,NULL);
InternetReadFile(hConnection,Buffer,sizeof(Buffer),&Read);
}
CloseHandle(hFile);
InternetCloseHandle(hConnection);
InternetCloseHandle(hSession);
}
方式2:
void FileDownload()
{
typedef int (__stdcall *LPFNDLLFUNC)(PVOID,char*,char*,int,char*); /* 函数原型*/
HINSTANCE hDLL;
LPFNDLLFUNC lpfnDllFunc;
char szUrlAddress[1024];
GetDlgItem(IDC_EDIT1)->GetWindowText(szUrlAddress,1024);
hDLL = LoadLibrary("urlmon.dll");
if (hDLL != NULL)
{
lpfnDllFunc = (LPFNDLLFUNC)GetProcAddress(hDLL, "URLDownloadToFileA");
if (!lpfnDllFunc)
{
FreeLibrary(hDLL);
return ;
}
else
{
(lpfnDllFunc)(NULL,szUrlAddress,"C:\123.exe",0,NULL);
FreeLibrary(hDLL);
}
}
::MessageBox(NULL, "下载完成!", "提示", MB_OK);
return;
}