title: How to use msxml with Visual Studio 2008 Express (no ATL classes) without becoming crazy?
body: <p>It is not really a question because I have already found a solution. It took me a lot of time, that's why I want to explain it here.</p>
  
  <p>Msxml is based on COM so it is not really easy to use in C++ even when you have helpful classes to deal with memory allocation issues. But writing a new XML parser would be much more difficult so I wanted to use msxml.</p>
  
  <p><strong>The problem:</strong></p>
  
  <p>I was able to find enough examples on the internet to use msxml with the help of <code>CComPtr</code> (smart pointer to avoid having to call Release() for each IXMLDOMNode manually), <code>CComBSTR</code> (to convert C++ strings to the COM format for strings) and <code>CComVariant</code>. This 3 helpful classes are ATL classes and need an <code>#include &lt;atlbase.h&gt;</code>.</p>
  
  <p>Problem: Visual Studio 2008 Express (the free version) doesn't include ATL.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Use <code>comutil.h</code> and <code>comdef.h</code>, which include some simple helper classes:</p>
  
  <ul>
  <li><code>_bstr_t</code> replaces more or less <code>CComBSTR</code></li>
  <li><code>_variant_t</code> replaces more or less <code>CComVariant</code></li>
  <li><code>_com_ptr_t</code> replaces indirectly <code>CComPtr</code> through the use of <code>_COM_SMARTPTR_TYPEDEF</code></li>
  </ul>
  
  <p><strong>Small example:</strong></p>
  
  <pre><code>#include &lt;msxml.h&gt;
  #include &lt;comdef.h&gt;
  #include &lt;comutil.h&gt;
  
  // Define some smart pointers for MSXML
  _COM_SMARTPTR_TYPEDEF(IXMLDOMDocument,     __uuidof(IXMLDOMDocument));     // IXMLDOMDocumentPtr
  _COM_SMARTPTR_TYPEDEF(IXMLDOMElement,      __uuidof(IXMLDOMElement));      // IXMLDOMElementPtr
  _COM_SMARTPTR_TYPEDEF(IXMLDOMNodeList,     __uuidof(IXMLDOMNodeList));     // IXMLDOMNodeListPtr
  _COM_SMARTPTR_TYPEDEF(IXMLDOMNamedNodeMap, __uuidof(IXMLDOMNamedNodeMap)); // IXMLDOMNamedNodeMapPtr
  _COM_SMARTPTR_TYPEDEF(IXMLDOMNode,         __uuidof(IXMLDOMNode));         // IXMLDOMNodePtr
  
  void test_msxml()
  {
      // This program will use COM
      CoInitializeEx(NULL, COINIT_MULTITHREADED);
  
      {
          // Create parser
          IXMLDOMDocumentPtr pXMLDoc;
          HRESULT hr = CoCreateInstance(__uuidof (DOMDocument), NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&amp;pXMLDoc);
          pXMLDoc-&gt;put_validateOnParse(VARIANT_FALSE);
          pXMLDoc-&gt;put_resolveExternals(VARIANT_FALSE);
          pXMLDoc-&gt;put_preserveWhiteSpace(VARIANT_FALSE);
  
          // Open file
          VARIANT_BOOL bLoadOk;
          std::wstring sfilename = L"testfile.xml";
          hr = pXMLDoc-&gt;load(_variant_t(sfilename.c_str()), &amp;bLoadOk);
  
          // Search for node &lt;testtag&gt;
          IXMLDOMNodePtr pNode;
          hr = pXMLDoc-&gt;selectSingleNode(_bstr_t(L"testtag"), &amp;pNode);
  
          // Read something
          _bstr_t bstrText;
          hr = pNode-&gt;get_text(bstrText.GetAddress());
          std::string sSomething = bstrText;
      }
  
      // I'm finished with COM
      // (Don't call before all IXMLDOMNodePtr are out of scope)
      CoUninitialize();
  }
  </code></pre>