简体   繁体   中英

Error C2065 'IXMLDOMDocument2Ptr': undeclared identifier

Here's a MS official example of how to use MSXML

void AddCollectionSample()  
{  
    IXMLDOMDocument2Ptr pIXMLDOMDocument2;  
    IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr; 
   ...
}

The code is simple, but doesn't compile on my Visual Studio 2019 with the error can't find the declaration of IXMLDOMDocument2Ptr.

I've already included these headers and lib.

#include "msxml2.h"  
#include "msxml6.h"  
#pragma comment(lib, "msxml6.lib")

I searched this IXMLDOMDocument2Ptr online and found out it's supposed to be under MSXML2 (a namespace). But my project doesn't recognize this namespace as well. Then I found it, you have to import this library, #import <msxml6.dll>

在此处输入图像描述

But this is supposed to be for C#. How about my C++ project?

Edit: I find this, https://www.codeproject.com/Articles/43309/How-to-create-a-simple-XML-file-using-MSXML-in-C

The first thing you need to do is add these two lines in the project stdafx.h file:

#import "MSXML4.dll" rename_namespace(_T("MSXML"))
#include <msxml2.h>

I modified this MSXML4.dll to my newly MSXML6.dll , but the project can't find it still.

The sample code is using _com_ptr_t , a COM smart pointer class template provided by Visual Studio. Yet the code provided is incomplete.

To declare a _com_ptr_t specialization for any given interface you would usually use the _COM_SMARTPTR_TYPEDEF macro. In this case the following declarations need to be added:

_COM_SMARTPTR_TYPEDEF(IXMLDOMDocument2, __uuidof(IXMLDOMDocument2));
_COM_SMARTPTR_TYPEDEF(IXMLDOMSchemaCollection2, __uuidof(IXMLDOMSchemaCollection2));

This declares the types IXMLDOMDocument2Ptr and IXMLDOMSchemaCollection2Ptr as specializations of _com_ptr_t , ready to be used. You'll need to #include <comip.h> , too.

An alternative would be to use the compiler specific #import directive . This generates header files from a type library (TLB), and by default includes the respective _COM_SMARTPTR_TYPEDEF declarations (see Primary type library header file ).

From this demo The first thing you need to do is add these two lines in the project stdafx.h file:

#import "MSXML6.dll" rename_namespace(_T("MSXML"))
#include <msxml2.h>

Then correct all the MSXML2 to MSXML , as we specified the name rename_namespace(_T("MSXML")) .

I didn't bother to do this, I at first tried namespace MSXML2=MSXML didn't work. So I just defined a MACRO #define MSXML2 MSXML . It compiled!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM