我想将一个xml文档从VBA模板传递给C ++ dll。我在这个dll中编写了函数:
extern "C" __declspec(dllexport) int __stdcall ProcessRequest(IXMLDOMDocument* request, IXMLDOMDocument* response);
int __stdcall ProcessRequest(IXMLDOMDocument* request, IXMLDOMDocument* response)
{
IXMLDOMElement* root = NULL;
request->get_documentElement(&root);
BSTR bstrVal = NULL;
root->get_text(&bstrVal);
::MessageBox(NULL, bstrVal, L"lol", MB_OK);
return 0;
}
我从VBA这样称呼它:
Public Declare Function ProcessRequest Lib "DllName" Alias "_ProcessRequest@8" (ByRef xml1 As DOMDocument, ByRef xml2 As DOMDocument) As Long
Public Sub ProcessRequestTest()
Dim xml1 As New DOMDocument
Dim xml2 As New DOMDocument
Dim x As Long
xml1.loadXML "<xml>lol</xml>"
x = ProcessRequest(xml1, xml2)
End Sub
然而,我收到了违规错误:request-&gt; get_documentElement(&amp; root); 那为什么会这样?这不是传递DOMDocument的正确方法吗?有没有办法,或者我应该只是传递字符串,来自巫婆dll会创建xml?
答案 0 :(得分:1)
你已经在C ++中将该函数声明为ByVal,而在VB声明语句中声明了ByRef。
要传递接口ByRef,您需要将其声明为IXMLDOMDocument**
E.g。你需要在C ++中使用它:
extern "C" __declspec(dllexport) int __stdcall ProcessRequest(IXMLDOMDocument** pprequest, IXMLDOMDocument** response);
int __stdcall ProcessRequest(IXMLDOMDocument** request, IXMLDOMDocument** ppresponse)