Microsoft KB Archive/316317

= PRB: Compiler Errors When You Use #import with XML in Visual C++ .NET =

Article ID: 316317

Article Last Modified on 6/28/2003

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-



This article was previously published under Q316317



SYMPTOMS
When you use MSXML parser dynamic-link libraries (DLLs) such as Msxml.dll, Msxml2.dll, Msxml3.dll, or Msxml4.dll to compile a Visual C++ .NET project with #import, you may receive the following compiler error messages:

c:\sample\test.cpp(12) : error C2872: 'IXMLDOMDocumentPtr' : ambiguous symbol

could be 'c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\comdefsp.h(1228) : _com_ptr_t<_IIID> IXMLDOMDocumentPtr' with [ _IIID=_com_IIID ]

or 'c:\sample\Debug\msxml3.tlh(226) : MSXML2::IXMLDOMDocumentPtr'

-or-

c:\sample\test.cpp(16) : error C2039: 'async' : is not a member of 'IXMLDOMDocument' c:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\MsXml.h(1845) : see declaration of 'IXMLDOMDocument'

-or-

c:\sample\test.cpp(27) : error C2660: 'IXMLDOMDocument::load' : function does not take 1 parameters

You may also see other C2039 and C2660 errors in the output. The workaround that is described in the following Knowledge Base article does not resolve the compiler errors with Microsoft Visual C++ .NET:

269194 PRB: Compiler Errors When You Use #import with XML



CAUSE
The &quot;ambiguous symbol&quot; error occurs when a smart pointer interface definition such as IXMLDOMDocumentPtr is defined in both the MSXML2 namespace (through #import) and the global namespace (through Msxml.h), so that the symbol is ambiguous. When the compiler resolves the interfaces, the compiler looks first in Msxml.h, which has different signatures than those that #import generates.



RESOLUTION
To resolve this problem, use the &quot;typelib&quot; namespace with MSXML parser DLLs to prefix the interface pointers and globally unique identifiers (GUIDs). To avoid the errors, explicitly qualify the interface declaration and GUIDs to remove the ambiguity, as follows:
 * 1) include 
 * 2) import 

void dump_com_error(_com_error &e);

int main(int argc, char* argv[]) {   CoInitialize(NULL); try {       MSXML2::IXMLDOMDocumentPtr pXMLDoc = NULL; HRESULT hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); pXMLDoc->async = VARIANT_FALSE; // default - true,

hr = pXMLDoc->loadXML(&quot; Abcdef &quot;); //hr = pXMLDoc->load(&quot;myData.xml&quot;); if (hr != VARIANT_TRUE) {           MSXML2::IXMLDOMParseErrorPtr  pError; pError = pXMLDoc->parseError; _bstr_t parseError =_bstr_t(&quot;At line &quot;)+ _bstr_t(pError->Getline) + _bstr_t(&quot;\n&quot;)+ _bstr_t(pError->Getreason); MessageBox(NULL,parseError, &quot;Parse Error&quot;,MB_OK); return 0; }       else {           MessageBox(NULL,pXMLDoc->xml, &quot;Loading Succeeded&quot;,MB_OK); }   }    catch(_com_error &e) {       dump_com_error(e); }   return 0; }

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); } NOTE: Msxml.dll uses the &quot;MSXML&quot; namespace. Msxml2.dll, Msxml3.dll and Msxml4.dll use the &quot;MSXML2&quot; namespace.



STATUS
This behavior is by design.



MORE INFORMATION
The following is a typical code sample that shows the use of the MSXML parser with Visual C++: using namespace MSXML2;
 * 1) import &quot;c:\winnt\system32\msxml4.dll&quot;

int main(int argc, char* argv[]) {   IXMLDOMDocumentPtr dom(__uuidof(DOMDocument40)); The #import feature includes a file named Comdef.h that indirectly includes Urlmon.h, which in turn includes Msxml.h. This is necessary because the end of Comdef.h includes Comdefsp.h, which defines many common GUIDs and XML interfaces. For example, the following sample causes Msxml.h to be included before the .tlh file: _COM_SMARTPTR_TYPEDEF(IXMLDocument, __uuidof(IXMLDocument)); _COM_SMARTPTR_TYPEDEF(IXMLElement, __uuidof(IXMLElement)); _COM_SMARTPTR_TYPEDEF(IXMLElementCollection, __uuidof(IXMLElementCollection)); _COM_SMARTPTR_TYPEDEF(IXMLElementNotificationSink, __uuidof(IXMLElementNotificationSink)); _COM_SMARTPTR_TYPEDEF(IXMLError, __uuidof(IXMLError)); With the MSXML 4.0 parser file, the Msxml4.tlh file is generated from #import.

Keywords: kbprb kbmsxmlnosweep KB316317

-

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

© Microsoft Corporation. All rights reserved.