【问题标题】:WScript.CreateObject returns empty when calling a C++ COM dll functionWScript.CreateObject 在调用 C++ COM dll 函数时返回空
【发布时间】:2017-12-16 20:01:10
【问题描述】:

我有一个 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 脚本代码无响应的原因。

【问题讨论】:

  • 为什么 [out] 不能使用 VB 脚本?我什至尝试过Dim Return: Return = Test.Add 1, 2, CInt("")WScript.Echo CStr(Return)。这甚至没有回应! :-(
  • VB 脚本不能从 COM 函数获取输出值吗?

标签: c++ string vbscript com


【解决方案1】:

你可以做的是改变 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

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 2016-08-03
    • 2021-04-14
    • 2011-02-05
    • 1970-01-01
    • 2018-01-27
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多