Microsoft Knowledge Base Email Alertz

Starting with MSXML version 3.0, the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 303326 - Last Review: November 24, 2001 - Revision: 1.2

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

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("POST", "http://www.server1.com/test.asp", 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.
#import "msxml3.dll"
using namespace MSXML2;     // For Msxml3.dll.

#include "stdio.h"

void dump_com_error(_com_error &e)
{
	printf("Error\n");
	printf("\a\tCode = %08lx\n", e.Error());
	printf("\a\tCode meaning = %s", e.ErrorMessage());
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());
	printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
	printf("\a\tDescription = %s\n", (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("POST", "http://www.server1.com/test1.asp", VARIANT_TRUE);			
		
		hr = pXMLHTTPReq->setRequestHeader("charset", "UTF-8");
		hr=pXMLHTTPReq->send("<?xml version=\"1.0\" encoding=\"UTF-8\"?><REQUEST>request1</REQUEST>");
		
		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, "readyState is COMPLETE ", "readystate", 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  (http://kbalertz.com/Feedback.aspx?kbNumber=290761/EN-US/ ) 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("POST", "http://www.server1.com/test1.asp", VARIANT_TRUE);			
   		
   	hr = pXMLHTTPReq->setRequestHeader("charset", "UTF-8");
   	hr=pXMLHTTPReq->send("<?xml version=\"1.0\" encoding=\"UTF-8\"?><REQUEST>request1</REQUEST>");

        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();	
				

APPLIES TO
  • Microsoft XML Parser 3.0, when used with:
    • the operating system: Microsoft Windows XP
  • Microsoft XML Parser 3.0 Service Pack 1, when used with:
    • the operating system: Microsoft Windows XP
Keywords: 
kbinfo KB303326
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate