Search code examples
javascriptc++visual-studioidl

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++. The JS code passes a string value to the c++/COM object.

[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. I added the following to the 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.

Does JS require a call by reference to get this or do I have to pass the one BSTR* param as [in,out]?

How can I get this to work? (getting a string value from C++/IDL to a JS caller?

What does the IDL have to look like and what should the JS code look like?


Solution

  • Javascript itself does not know how to handle values returned through parameters. You have to explicitly declare which parameter is the return value otherwise COM will simply return the HRESULT. You can do this with the following.

    [id(3), helpstring("method GetValue")]  HRESULT GetValue([out, retval] BSTR *nicknames);