学习笔记(1)--URLDownloadToFile()函数的使用

使用URLDownloadToFile()函数下载文件

#include
#include
#include 
#pragma comment (lib,"Urlmon.lib")

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
     

	HRESULT hret =URLDownloadToFile(NULL,"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558373369721&di=c1adab90d9465b976adb823594197df9&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F18%2F20161018051842_Rm4dM.jpeg","2.jpg",0,NULL);

	
	if (hret==S_OK)
	{
     
		MessageBox(NULL,"下载成功","Tip",MB_OK);

	}
	else
	{
     
	MessageBox(NULL,"下载失败","Tip",MB_OK);

	}

	ShellExecuteA(NULL,"open","2.jpg",0,0,1);
	return 0;
}

HRESULT URLDownloadToFile(
LPUNKNOWN pCaller,
LPCTSTR szURL,
LPCTSTR szFileName,
DWORD dwReserved,
LPBINDSTATUSCALLBACK lpfnCB
);

参数

pCaller: Pointer to the controlling IUnknown interface of the calling
Microsoft ActiveX component (if the caller is an ActiveX component).
//该参数为 ActiveX 控件的接口指针,如果当前程序不是 ActiveX 控件则写 NULL 就可以了。
szURL: Pointer to a string value containing the URL to be downloaded. Cannot be set to NULL. //该参数为要下载的 url 地址,不能为空。
szFileName: Pointer to a string value containing the name of the file to create for bits that come from the download.
//下载文件后,保存的文件名字,包含文件的具体路径信息。
dwReserved: Reserved. Must be set to 0. //保留字段,必需为0。
lpfnCB: Pointer to the caller’s IBindStatusCallback interface. URLDownloadToFile calls this interface’s
IBindStatusCallback::OnProgress method on a connection activity,
including the arrival of data. IBindStatusCallback::OnDataAvailable is
never called. //下载进度状态回调接口的指针。如果要实时监视下载文件的状态那么就要用到这个参数了。
返回值: S_OK : 成功下载; E_OUTOFMEMORY:缓冲区长度不够,或者没有足够的内存来完成下载的操作; INET_E_DOWNLOAD_FAILURE:指定的资源或者回调接口有问题

ShellExecute(
hWnd: HWND; {指定父窗口句柄}
Operation: PChar; {指定动作, 譬如: open、runas、print、edit、explore、find [2] }
FileName: PChar; {指定要打开的文件或程序}
Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}
Directory: PChar; {缺省目录}
ShowCmd: Integer {打开选项}
): HINST;

ShowCmd 参数可选值:SW_HIDE = 0; {隐藏} SW_SHOWNORMAL = 1; {用最近的大小和位置显示, 激活}
SW_NORMAL = 1; {同 SW_SHOWNORMAL} SW_SHOWMINIMIZED = 2; {最小化, 激活}
SW_SHOWMAXIMIZED = 3; {最大化, 激活} SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}
SW_SHOWNOACTIVATE = 4; {用最近的大小和位置显示, 不激活} SW_SHOW = 5; {同
SW_SHOWNORMAL} SW_MINIMIZE = 6; {最小化, 不激活} SW_SHOWMINNOACTIVE = 7; {同
SW_MINIMIZE} SW_SHOWNA = 8; {同 SW_SHOWNOACTIVATE} SW_RESTORE = 9; {同
SW_SHOWNORMAL} SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL} SW_MAX = 10; {同
SW_SHOWNORMAL}

这是一个win32的程序,
其中使用了几个win32的比较有意思的API,
学习这些API并不是为了入门Windows编程,
只是觉得比较有意思,
也大致了解了一下Windows编程,
其实C语言也能干很多事。

你可能感兴趣的:(C/C++学习笔记,c++)