Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Source: XMLOverHTTP.cpp

This C/C++ code performs the following steps:

  1. Creates an IXMLHTTPRequest object, pIXMLHTTPRequest.
  2. Uses the open method and the send method to request the contact information for "John Doe".
  3. Uses the responseXML property to retrieve the XML data returned from the server.
  4. Displays the XML data to the console.

C/C++ Source File (XMLOverHTTP.cpp)

#include <stdio.h>
#import <msxml5.dll>
using namespace MSXML2;

int main(int argc, char* argv[])
{
   CoInitialize(NULL);

   IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
   IXMLDOMDocumentPtr pXMLDoc = NULL;
   BSTR bstrString = NULL;
   HRESULT hr;

   try {
      hr=pIXMLHTTPRequest.CreateInstance(__uuidof(XMLHTTP50));
      SUCCEEDED(hr) ? 0 : throw hr;

      hr=pIXMLHTTPRequest->open("GET", 
             "http://localhost/sxh/contact.asp?SearchID=John Doe", 
              false);
      SUCCEEDED(hr) ? 0 : throw hr;

      hr=pIXMLHTTPRequest->send();
      SUCCEEDED(hr) ? 0 : throw hr;

      pXMLDoc=pIXMLHTTPRequest->responseXML;
      bstrString=pXMLDoc->xml;
      if(bstrString)
      {
         printf("%S\n", bstrString);
         SysFreeString(bstrString);
         bstrString = NULL;
      }

   } 
   catch (...) {
      printf("error: %x\n",hr);
   }

   if(bstrString)
      SysFreeString(bstrString);
   if (pIXMLHTTPRequest)
      pIXMLHTTPRequest.Release();
   if (pXMLDoc)
      pXMLDoc.Release();

   CoUninitialize();
   return 0;
}

To add the XMLOverHTTP source code to the project

  1. Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the file XMLOverHTTP.cpp.
  2. Copy the C/C++ source code above, and paste it into the source file you just created.

Next, we'll set up a virtual directory. You must complete this step before you build and run the application.