PRB: Compiler Errors When You Use #import with XML


When you use the #import feature of Visual C++ with the Msxml.dll, Msxml2.dll, or Msxml3.dll file, you may receive the following compiler error messages:
error C2872: 'DOMDocument' : ambiguous symbol
warning C4099: 'DOMDocument' : type name first seen using 'class' now seen using 'struct'
error C2011: 'IXMLDOMImplementation' : 'struct' type redefinition
You may see many other C4099 and C2011 errors in the output as well.


The #import feature includes a file named Comdef.h, which indirectly includes files that include Msxml.h. This is necessary because Comdef.h defines some common GUIDs and contains the following:
_COM_SMARTPTR_TYPEDEF(IXMLDocument, __uuidof(IXMLDocument));
_COM_SMARTPTR_TYPEDEF(IXMLElement, __uuidof(IXMLElement));
_COM_SMARTPTR_TYPEDEF(IXMLElementCollection, __uuidof(IXMLElementCollection));
_COM_SMARTPTR_TYPEDEF(IXMLElementNotificationSink, __uuidof(IXMLElementNotificationSink));
Because these types are referenced, it is important for the Msxml.h file to be included. This causes redefinitions when #import'ing on the XML dynamic-link library (DLL). The redefinition errors occur when you use the no_namespace attribute for #import. Here is an example:
#import "c:\winnt\system32\msxml.dll" no_namespace 
int main(int argc, char* argv[])
IXMLDOMDocumentPtr dom(__uuidof(DOMDocument));
The "ambiguous symbol" error can occur when you use a namespace with #import, and use the Msxml.h header file from the Platform Software Development Kit (SDK) or a newer preview XML SDK from the Web. Because the newer Msxml.h defines DOMDocument in the global namespace, and the #import defines it in another namespace, the symbol is ambiguous. Here is the code to reproduce the problem:
#import "c:\winnt\system32\msxml.dll"
using namespace MSXML;
int main(int argc, char* argv[])
IXMLDOMDocumentPtr dom(__uuidof(DOMDocument));


The "no_namespace" attribute cannot be used with #import with the XML DLL.

The "ambiguous symbol" error can be avoided by using the Msxml.h header file from the Visual C++ 6.0 Include folder. This Msxml.h file contains only the old XML definitions that are necessary to satisfy Comdef.h, but doesn't redefine such newer symbols as DOMDocument.

If you use the Platform SDK or XML SDK header files, you can add those Include folders after the Visual C++ Include folder when you use #import. To do this, in Visual C++, on the Tools menu, click Options, and then click the Directories tab.

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:
305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

316317 PRB: Compiler Errors When Using #import with XML in Visual C++ .NET

Artikelnummer: 269194 – Letzte Überarbeitung: 22.06.2014 – Revision: 1