一段能够下载文件的源代码
// 在工程中插手 Urlmon.lib
//—————————————————————————
#include <vcl.h>
#include <windows.h>
#include <Urlmon.h>
#pragma hdrstop
#include "Unit1.h"
//—————————————————————————
cImpMonikerCallback objImpCallback;
//—————————————————————————
#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LPUNKNOWN pCaller;
pCaller = NULL;
DWORD dwResv = 0;
objImpCallback.lpfnCB = (IBindStatusCallback *)&objImpCallback;
HRESULT hRet=URLDownloadToFile(pCaller,"http://127.0.0.1:30000/download/icon.zip","d:\\temp\\icon.zip",dwResv,objImpCallback.lpfnCB);
if(hRet==S_OK)
MessageBox(NULL,"乐成下载文件","",MB_OK);
return 0;
}
//—————————————————————————
cImpMonikerCallback::cImpMonikerCallback()
{
m_cRef = 1;
m_pbinding = NULL;
};
//—————————————————————————
cImpMonikerCallback::~cImpMonikerCallback()
{
if(m_pbinding)
m_pbinding->Release();
};
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::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) cImpMonikerCallback::AddRef()
{
return m_cRef++;
}
//—————————————————————————
STDMETHODIMP_(ULONG) cImpMonikerCallback::Release()
{
if(–m_cRef==0)
{
delete this;
return 0;
}
return m_cRef;
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnStartBinding(DWORD grfBSCOption, IBinding* pbinding)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::GetPriority(LONG* pnPriority)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnProgress(ULONG ulProgress, ULONG ulProgressMax,ULONG ulStatusCode, LPCWSTR szStatusText)
{
return(NOERROR);
};
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pfmtetc,STGMEDIUM* pstgmed)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnObjectAvailable(REFIID riid, IUnknown* punk)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnLowResource(DWORD dwReserved)
{
return(NOERROR);
}
//—————————————————————————
STDMETHODIMP cImpMonikerCallback::OnStopBinding(HRESULT hrStatus, LPCWSTR szText)
{
return(NOERROR);
}
//—————————————————————————
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
class cImpMonikerCallback:public IBindStatusCallback
{
DWORD m_cRef;
IBinding* m_pbinding;
public:
cImpMonikerCallback();
~cImpMonikerCallback();
STDMETHODIMP QueryInterface(REFIID riid,void ** ppv);
STDMETHODIMP_(ULONG) AddRef();// { return m_cRef++; }
STDMETHODIMP_(ULONG) Release();
STDMETHODIMP GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo);
STDMETHODIMP OnStartBinding(DWORD grfBSCOption, IBinding* pbinding);
STDMETHODIMP GetPriority(LONG* pnPriority);
STDMETHODIMP OnProgress(ULONG ulProgress, ULONG ulProgressMax,ULONG ulStatusCode, LPCWSTR szStatusText);
STDMETHODIMP OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pfmtetc,STGMEDIUM* pstgmed);
STDMETHODIMP OnObjectAvailable(REFIID riid, IUnknown* punk);
STDMETHODIMP OnLowResource(DWORD dwReserved);
STDMETHODIMP OnStopBinding(HRESULT hrStatus, LPCWSTR szText);
LPBINDSTATUSCALLBACK lpfnCB;
PSTR sURL;
PSTR sFile;
char cSysDir[MAX_PATH];
DWORD dwPlatform;
};