Download using Urlopenstream under C + +

Source: Internet
Author: User

Although Urldownloadtofile is convenient, you may want to use Urlopenstream to achieve more complex requirements.

But it's still a bit tricky to check MSDN, and here's the sample code directly:

1#include <iostream>2#include <cstdio>3#include <UrlMon.h>4 #pragmaComment (lib, "Urlmon.lib")5#include <tchar.h>6#include <fstream>7 8 classMyListener: PublicIBindStatusCallback9 {Ten  Public: One  A stdmethod (onstartbinding) ( - DWORD dwreserved, -IBinding __RPC_FAR *PIB) the     { -         returnE_NotImpl; -     } -  + stdmethod (getpriority) ( -LONG __RPC_FAR *pnpriority) +     { A         returnE_NotImpl; at     } -  - stdmethod (Onlowresource) ( - DWORD reserved) -     { -         returnE_NotImpl; in     } -  to stdmethod (onprogress) ( +ULONG ulprogress,//Current Size -ULONG ulProgressMax,//Total size the ULONG Ulstatuscode, * lpcwstr szStatusText) $     {Panax Notoginseng         if(ulProgressMax! =0) -         { theStd::cout << ulprogress *100.0/ulProgressMax <<"%\t"<< Ulstatuscode <<"\ n"; +         } A         returnS_OK; the     } +  - stdmethod (onstopbinding) ( $ HRESULT HRESULT, $ lpcwstr Szerror) -     { -         returnE_NotImpl; the     } - Wuyi stdmethod (getbindinfo) ( theDWORD __RPC_FAR *GRFBINDF, -Bindinfo __RPC_FAR *pbindinfo) Wu     { -         returnE_NotImpl; About     } $  - stdmethod (ondataavailable) ( - DWORD GRFBSCF, - DWORD dwsize, AFORMATETC __RPC_FAR *Pformatetc, +STGMEDIUM __RPC_FAR *pstgmed) the     { -             //bscf_firstdatanotification = 0x00000001, $             //bscf_intermediatedatanotification = 0x00000002, the             //bscf_lastdatanotification = 0x00000004, the         //possible for bscf_firstdatanotification, Bscf_firstdatanotification & Bscf_lastdatanotification, BSCF_ Intermediatedatanotification, Bscf_lastdatanotification the         //that could be a package, two packages, or multiple packages the         if(GRFBSCF &bscf_firstdatanotification) -         { inPstgmed->pstm->read (Mdata, dwsize, &msize); the         } the         Else About         { the ULONG size; thePstgmed->pstm->read (Mdata + msize, dwsize, &size); theMsize + =size; +Std::cout << dwsize <<"\ t"<< size <<"\ t"<< msize <<"\ n"; -         } the Bayi          the         returnS_OK; the     } -  - stdmethod (onobjectavailable) ( the REFIID riid, theIUnknown __RPC_FAR *punk) the     { the         returnE_NotImpl; -     } the  the stdmethod_ (ULONG, AddRef) () the     {94         return 0; the     } the  the stdmethod_ (ULONG, Release) ()98     { About         return 0; -     }101 102 Stdmethod (QueryInterface) (103 REFIID riid,104         void__rpc_far *__rpc_far *ppvobject) the     {106         returnE_NotImpl;107     }108 109     voidSaveChar*fileName) the     {111 File.Open (FileName, std::ios::binary); the File.write (Mdata, msize);113 file.close (); the     } the Private: the std::ofstream file;117 ULONG msize;118     Charmdata[ -*1024x768];119 }; - 121 122 intMainintargcChar*argv[])123 {124     Charurl[ the]; the     Charfilename[ the];127  -mylistener* listener =NewMyListener; $HRESULT result = Urlopenstream (0Url0, listener);141             if(Result = =S_OK)142             {143Listener->Save (fileName);144             }145             Else146             {147Std::cout << Result <<"failure!\n";148             }149 the 153     DeleteListener;154 155System"Pause");156}

Download using Urlopenstream under C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.