程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 一段能夠下載文件的源代碼

一段能夠下載文件的源代碼

編輯:關於C++

// 在工程中加入 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 &brvbar;&brvbar; 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;
};

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved