带进度下载文件
本帖最后由 htpidk 于 2020-5-25 12:46 编辑URLDownloadToFile这个函数以前最后一个参数一般都是传NULL,今天在网上看到可以传一个类似于COM对象的指针从而实现进度百分比,挺有意思的,参考网上代码,花了点时间改了下。
#include "stdafx.h"
class xiazai:public IBindStatusCallback{
public:
xiazai(){};
virtual ~xiazai(){};
virtual ULONG__stdcall AddRef(){return E_NOTIMPL;};
virtual HRESULT __stdcall QueryInterface(REFIID riid,void **ppvObject){return E_NOTIMPL;};
virtual ULONG __stdcall Release(){return E_NOTIMPL;};
virtual HRESULT __stdcall GetBindInfo(DWORD *grfBINDF,BINDINFO *pbindinfo){return E_NOTIMPL;};
virtual HRESULT __stdcall GetPriority(long *pnPriority){return E_NOTIMPL;};
virtual HRESULT __stdcall OnDataAvailable(DWORD grfBSCF,DWORD dwSize,FORMATETC *pformatetc,STGMEDIUM *pstgmed){return E_NOTIMPL;};
virtual HRESULT __stdcall OnLowResource(DWORD reserved){return E_NOTIMPL;};
virtual HRESULT __stdcall OnObjectAvailable(REFIID riid,IUnknown *punk){return E_NOTIMPL;};
virtual HRESULT __stdcall OnProgress(unsigned long ulProgress,unsigned long ulProgressMax,unsigned long ulStatusCode,LPCWSTR szStatusText);
virtual HRESULT __stdcall OnStartBinding(DWORD dwReserved,IBinding *pib){return E_NOTIMPL;};
virtual HRESULT __stdcall OnStopBinding(HRESULT hresult,LPCWSTR szError){return E_NOTIMPL;};
};
HRESULT __stdcall xiazai::OnProgress(unsigned long ulProgress,unsigned long ulProgressMax,unsigned long ulStatusCode,LPCWSTR szStatusText){
if(ulProgress>0){
system("cls");
cout<<_T("文件总大小:")<<_T("\t")<<ulProgressMax<<_T("个字节")<<endl;
cout<<_T("当前下载:")<<_T("\t")<<ulProgress*100.0/ulProgressMax<<_T("%")<<endl;
//如果是UNICODE,需改成wcout
}
return S_OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
xiazai ceshi;
URLDownloadToFile(NULL,_T("https://down.52pojie.cn/Tools/Packers/VMProtect_Professional_v2.13.8.rar"),_T("C:\\Users\\69020100204967\\Desktop\\pe.zip"),0,&ceshi);
system("pause");
return 0;
}
[ 可以收藏一下。 厉害,我以为最后一个参数必须传0(NULL),还有这用处 可以支持一下 看不动系列。
页:
[1]