WScript.CreateObject returns 调用 C++ COM dll 函数时为空

WScript.CreateObject returns empty when calling a C++ COM dll function

我有一个 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);

这在这里很有意义,因为它 在语义上是 一个 return 值。有关这方面的信息,请参阅 retval attribute 文档。

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

ret = Add(1, 2)

否则,请在 VBScript 中查看有关 byref 的更多信息:ByRef and ByVal in VBScript