How to Use the urlopenstream function-Reply to "Tianze"

Source: Internet
Author: User
Urlopenstream is similar to urldownloadtofile, and is a com function for downloading files;

The former is downloaded to the istream stream, and the latter is directly downloaded to the specified path. It is not as convenient as the latter.

They are all declared in the urlmon unit. In this example, uses ActiveX is also required because the istream interface is used.

 
Function urlopenstream (P1: iunknown; {interface, don't use it, just give nil} P2: pwidechar; {path to download} P3: DWORD; {parameters not used yet, it must be 0} P4: ibindstatuscallback {interface, the downloaded data must be given to it; we need to implement it}): hresult; stdcall; {returning s_ OK indicates success, this example uses the succeeded function to determine}

  

The ibindstatuscallback interface has eight methods (or events). If they are not used, a simple implementation is required;
We mainly implement ondataavailable, because the downloaded data is returned through the stgmed parameter.

The following is the implementation and TestCode:

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, urlmon, ActiveX; Type tform1 = Class (tform) button1: tbutton; procedure button1click (Sender: tobject); end; identifier = Class (tinterfacelist, identifier) Public Function onstartbinding (dwreserved: DWORD; PIB: ibinding): hresult; stdcall; function getpriority (out n Priority): hresult; stdcall; function onlowresource (Reserved: DWORD): hresult; stdcall; function onprogress (ulprogress, ulprogressmax, ulstatuscode: ulong; lead: lpcwstr): hresult; stdcall; function onstopbinding (hresult: hresult; szerror: lpcwstr): hresult; stdcall; function getbindinfo (Out grfbindf: DWORD; var bindinfo: tbindinfo): hresult; stdcall; function Merge (grfbscf: dwor D; dwsize: DWORD; formatetc: pformatetc; stgmed: pstgmedium): hresult; stdcall; function onobjectavailable (const IID: tguid; punk: iunknown): hresult; stdcall; end; vaR form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); var URL: string; mybindstatuscallback: ibindstatuscallback; begin button1.caption: = 'download... '; button1.enabled: = false; Url: = 'HTTP: // files. cnblo Export mybindstatuscallback: = tbindstatuscallback. Create; If succeeded (urlopenstream (nil, pchar (URL), 0, mybindstatuscallback) Then button1.caption: = 'Download complete! 'Else button1.caption: = 'download failed! '; Button1.enabled: = true; end; {tbindstatuscallback} function tbindstatuscallback. getbindinfo (Out grfbindf: DWORD; var bindinfo: tbindinfo): hresult; begin result: = s_ OK; end; function tbindstatuscallback. getpriority (Out npriority): hresult; begin result: = s_ OK; end; function tbindstatuscallback. ondataavailable (grfbscf, dwsize: DWORD; formatetc: pformatetc; stgmed: pstgmedium): hresult; var stream: istream; mem: tmemorystream; begin if dwsize> 0 then begin stream: = istream (stgmed. STM); mem: = tmemorystream. create; mem. setsize (dwsize); stream. read (mem. memory, dwsize, nil); // showmessage (inttostr (mem. size); mem. savetofile ('C: \ temp \ pmark_1.rar '); mem. free; Result: = s_ OK; end else result: = e_abort; end; function tbindstatuscallback. onlowresource (Reserved: DWORD): hresult; begin result: = s_ OK; end; function tbindstatuscallback. onobjectavailable (const IID: tguid; punk: iinterface): hresult; begin result: = s_ OK; end; function tbindstatuscallback. onprogress (ulprogress, ulprogressmax, ulstatuscode: ulong; szstatustext: lpcwstr): hresult; begin // if you need to download the progress, write the code result: = s_ OK; end function; tbindstatuscallback. onstartbinding (dwreserved: DWORD; PIB: ibinding): hresult; begin result: = s_ OK; end; function tbindstatuscallback. onstopbinding (hresult: hresult; szerror: lpcwstr): hresult; begin result: = s_ OK; end.
 
  
 

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.