文件下载
我现在使用的下载代码如下:
//进度显示for(bar->Position=0;bar->Position< bar->Max;bar->StepIt()){ //下载文件 URLDownloadToFile(NULL,"http://localhost/Demo4/install.exe","download\\install.exe",0,NULL); lab->Caption="下载成功";}
#include <UrlMon.h>#pragma link "C:\\Program Files\\Borland\\CBuilder6\\\Lib\\Psdk\\urlmon.lib"class TCrnCallback : public IBindStatusCallback{private: TProgressBar *m_pb; DWORD m_cRef; STDMETHODIMP QueryInterface(REFIID riid, void **ppv); STDMETHODIMP_(ULONG) AddRef(); STDMETHODIMP_(ULONG) Release(); STDMETHODIMP GetBindInfo(DWORD *grfBINDF, BINDINFO *bindinfo); STDMETHODIMP GetPriority(LONG *nPriority); STDMETHODIMP OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *formatetc, STGMEDIUM *stgmed); STDMETHODIMP OnLowResource(DWORD reserved); STDMETHODIMP OnObjectAvailable(REFIID iid, IUnknown *punk); STDMETHODIMP OnStartBinding(DWORD dwReserved, IBinding *pib); STDMETHODIMP OnStopBinding(HRESULT hresult, LPCWSTR szError); STDMETHODIMP OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText);public: TCrnCallback(TProgressBar *pb): m_pb(pb) {m_cRef = 1;};};STDMETHODIMP TCrnCallback::QueryInterface(REFIID riid, void **ppv){ *ppv = NULL; if (riid == IID_IUnknown || riid == IID_IBindStatusCallback) { *ppv = this; AddRef(); return S_OK; } return E_NOINTERFACE;}STDMETHODIMP_(ULONG) TCrnCallback::AddRef(){ return m_cRef++;}STDMETHODIMP_(ULONG) TCrnCallback::Release(){ if(--m_cRef == 0) { delete this; return 0; } return m_cRef;}STDMETHODIMP TCrnCallback::GetBindInfo(DWORD *grfBINDF, BINDINFO *bindinfo){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::GetPriority(LONG *nPriority){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *formatetc, STGMEDIUM *stgmed){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnLowResource(DWORD reserved){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnObjectAvailable(REFIID iid, IUnknown *punk){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnStartBinding(DWORD dwReserved, IBinding *pib){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnStopBinding(HRESULT hresult, LPCWSTR szError){ return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText){ m_pb->Max = ulProgressMax; m_pb->Position = ulProgress; return S_OK;}void __fastcall TForm1::Button1Click(TObject *Sender){ TCrnCallback *pCallback = new TCrnCallback(ProgressBar1); try { URLDownloadToFile(NULL, TEXT("http://www.ccrun.com/temp/htmlinres.zip"), TEXT("C:\\123.zip"), 0, pCallback); } __finally { delete pCallback; } ShowMessage("完毕");}