Microsoft KB Archive/303326

= INFO: Visual C++ Application Using ServerXMLHTTP or XMLHTTP in Async Mode Must Pump Messages =

Article ID: 303326

Article Last Modified on 11/24/2001

-

APPLIES TO


 * Microsoft XML Core Services 4.0, when used with:
 * Microsoft Windows XP Professional
 * Microsoft XML Parser 3.0 Service Pack 1, when used with:
 * Microsoft Windows XP Professional

-



This article was previously published under Q303326



SUMMARY
Starting with MSXML version 3.0, the XMLHTTP request object uses Urlmon.dll. If you set the async parameter to VARIANT_TRUE when you call the open method, URLMON uses the message queue mechanism to notify the application when data becomes available, and the readyState property of the XMLHTTP request object is changed, as in the following example: hr=pXMLHTTPReq->open(&quot;POST&quot;, &quot;http://www.server1.com/test.asp&quot;, VARIANT_TRUE); In a Visual C++ application, you must use a message pump to receive and dispatch messages. If the application does not handle the incoming messages properly, the application may stop responding (hang) because the readystate property of the XMLHTTP request object is not changed.

NOTE: This issue is specific to Visual C++ developers.

NOTE: This information is specific to Windows XP. By default, Windows XP ships with MSXML3.0 Service Pack 2 (SP2).



MORE INFORMATION
With the MSXML 2.5 parser that ships with Microsoft Windows 2000, no such message pump is needed. With Windows XP, MSXML 3.0 SP2 is installed in Replace mode, so you may encounter this problem.

The following is a sample implementation of the message pump with the XMLHTTP request object. To test it, paste the code into a .cpp file, then compile and run the file as a Win32 Console application project. using namespace MSXML2;    // For Msxml3.dll.
 * 1) import &quot;msxml3.dll&quot;


 * 1) include &quot;stdio.h&quot;

void dump_com_error(_com_error &e) {   printf(&quot;Error\n&quot;); printf(&quot;\a\tCode = %08lx\n&quot;, e.Error); printf(&quot;\a\tCode meaning = %s&quot;, e.ErrorMessage); _bstr_t bstrSource(e.Source); _bstr_t bstrDescription(e.Description); printf(&quot;\a\tSource = %s\n&quot;, (LPCSTR) bstrSource); printf(&quot;\a\tDescription = %s\n&quot;, (LPCSTR) bstrDescription); }

int main {   HRESULT hr = CoInitialize(NULL); try{ IXMLHTTPRequestPtr pXMLHTTPReq = NULL; hr=pXMLHTTPReq.CreateInstance(__uuidof(XMLHTTP30)); //Uncomment the following lines if you are using ServerXMLHTTPRequest. //IServerXMLHTTPRequestPtr pXMLHTTPReq = NULL; //hr=pXMLHTTPReq.CreateInstance(__uuidof(ServerXMLHTTP30)); hr=pXMLHTTPReq->open(&quot;POST&quot;, &quot;http://www.server1.com/test1.asp&quot;, VARIANT_TRUE); hr = pXMLHTTPReq->setRequestHeader(&quot;charset&quot;, &quot;UTF-8&quot;); hr=pXMLHTTPReq->send(&quot;request1&quot;); long readyState = READYSTATE_UNINITIALIZED; MSG msg; while (readyState != READYSTATE_COMPLETE) {                         //Without this message pump, readyState does not change. if (PeekMessage(&msg, 0, 0 ,0, PM_REMOVE)) {               TranslateMessage (&msg) ; DispatchMessage (&msg) ; }           readyState = pXMLHTTPReq->GetreadyState; }       MessageBox(NULL, &quot;readyState is COMPLETE &quot;, &quot;readystate&quot;, MB_OK); long status= pXMLHTTPReq->Getstatus; }   catch(_com_error &e) {       dump_com_error(e); }   return 0; }

NOTE: Asynchronous processing in practice is more complicated. You should test your application in synchronous mode before you try to use asynchronous mode.

When you use the SeverXMLHTTPRequest object, you must run the Proxycfg utility to set the proxy information. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

290761 Frequently Asked Questions about ServerXMLHTTP

ServerXMLHTTP does not use Urlmon.dll. However, when you use ServerXMLHTTP in asynchronous mode, you also need this message pump because the parser fires the event by posting messages back to the thread.

ServerXMLHTTP also includes a method called waitForResponse that is not available with XMLHTTP. The waitForResponse waits while an asynchronous request is processed. The waitForResponse method pumps window messages automatically, so the client's application code does not need to do this. This allows the application code to be simplified, as follows: hr=pXMLHTTPReq.CreateInstance(__uuidof(ServerXMLHTTP30)); hr=pXMLHTTPReq->open(&quot;POST&quot;, &quot;http://www.server1.com/test1.asp&quot;, VARIANT_TRUE); hr = pXMLHTTPReq->setRequestHeader(&quot;charset&quot;, &quot;UTF-8&quot;); hr=pXMLHTTPReq->send(&quot;request1&quot;);

VARIANT Timeout; VariantInit(&Timeout); Timeout.vt = VT_I4; Timeout.lVal = 60; // Timeout value of 60 seconds.

VARIANT_BOOL bSucceeded; //Passing vtMissing as the first parameter sets the timeout value to infinite. hr = pXMLHTTPReq->waitForResponse(Timeout, &bSucceeded); long status= pXMLHTTPReq->Getstatus;

Additional query words: Windows XP SeverXMLHttp SeverXMLHttpRequest XMLHttp XMLHttpRequest async

Keywords: kbinfo KB303326

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.