gpt4 book ai didi

c++ - WScript.CreateObject 在调用 C++ COM dll 函数时返回空

转载 作者:行者123 更新时间:2023-11-28 05:04:53 24 4
gpt4 key购买 nike

我有一个 C++ COM dll 项目,当从 VB 脚本文件中的 COM 对象调用以下函数时,它工作正常。

[id(1)] HRESULT ShowMessage([in] BSTR sMessage, BSTR sTitle); //<< .IDL File

STDMETHOD(ShowMessage)(BSTR sMessage, BSTR sTitle); //<< Header File

STDMETHODIMP CFoo::ShowMessage(BSTR sMessage, BSTR sTitle) //<< C++ Source File
{
MessageBox(0, CString(sMessage), CString(sTitle), 0);
return S_OK;
}

当我像这样从 VB 脚本中调用上面的函数时,它工作正常:

Dim Test: Set Test = WScript.CreateObject("VbsEvents.dll")
Test.ShowMessage "Hello World!", "Windows Script Host"

但是,如果我像下面这样声明函数:

[id(2)] HRESULT Add([in] int Value1, int Value2, [out] int *ReturnValue); //<< .IDL File

STDMETHOD(Add)(int Value1, int Value2, int *ReturnValue); //<< Header File

STDMETHODIMP CFoo::Add(int Value1, int Value2, int *ReturnValue) //<< C++ Source File
{
*ReturnValue = Value1 + Value2;
return S_OK;
}

并从 VB 脚本调用它,例如:

Dim Return: Test.Add 1, 2, CInt(Return)
WScript.Echo CStr(Return)

我一直没有收到任何回应,我希望这会回应 3。我不明白为什么这个函数在 VB 脚本中不起作用。

如果您能帮助找出导致此 VB 脚本代码不回显的原因,我们将不胜感激。

最佳答案

你可以做的是改变这个 IDL 签名

[id(2)] HRESULT Add([in] int Value1, int Value2, [out] int *ReturnValue);

对此

[id(2)] HRESULT Add([in] int Value1, int Value2, [out, retval] int *ReturnValue);

这在这里很有意义,因为它在语义上是一个返回值。查看retval attribute有关此信息的文档。

然后你可以在 VBScript 中这样调用它:

ret = Add(1, 2)

否则,请检查此以获取有关 VBScript 中 byref 的更多信息:ByRef and ByVal in VBScript

关于c++ - WScript.CreateObject 在调用 C++ COM dll 函数时返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com