[英]How do I return a string value from C++ to javascript (windows/Visual studio 2008)?
I have the following IDL which works fine for passing a string value from JS to C++.我有以下 IDL,它可以很好地将字符串值从 JS 传递到 C++。 The JS code passes a string value to the c++/COM object. JS 代码将字符串值传递给 c++/COM 对象。
[id(1), helpstring("method DoSomething")] HRESULT DoSomething([in] BSTR otlToken);
I now need to add another method to return a string to a javascript caller.我现在需要添加另一个方法来将字符串返回给 javascript 调用者。 I added the following to the IDL:我在 IDL 中添加了以下内容:
[id(3), helpstring("method GetValue")] HRESULT GetValue([out] BSTR *nicknames);
The developer who is working on the JS side says he gets a message about wrong number of arguments and other things depending on if he tries to call the method or access it as a property.在 JS 方面工作的开发人员说,他收到一条关于错误数量的参数和其他事情的消息,这取决于他是尝试调用该方法还是将其作为属性访问。
Does JS require a call by reference to get this or do I have to pass the one BSTR* param as [in,out]? JS 是否需要通过引用调用来获取它,还是我必须将一个 BSTR* 参数作为 [in,out] 传递?
How can I get this to work?我怎样才能让它发挥作用? (getting a string value from C++/IDL to a JS caller? (从 C++/IDL 获取字符串值到 JS 调用者?
What does the IDL have to look like and what should the JS code look like? IDL 必须是什么样子,JS 代码应该是什么样子?
Javascript itself does not know how to handle values returned through parameters. Javascript 本身不知道如何处理通过参数返回的值。 You have to explicitly declare which parameter is the return value otherwise COM will simply return the HRESULT.您必须明确声明哪个参数是返回值,否则 COM 将简单地返回 HRESULT。 You can do this with the following.您可以使用以下方法执行此操作。
[id(3), helpstring("method GetValue")] HRESULT GetValue([out, retval] BSTR *nicknames);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.