I need to call into a Win32 API to get a series of strings, and I would like to return an array of those strings to JavaScript. This is for script that runs on local machine for administration scripts, not for the web browser.
My IDL file for the COM object has the interface that I am calling into as:
HRESULT GetArrayOfStrings([out, retval] SAFEARRAY(BSTR) * rgBstrStringArray);
The function returns correctly, but the strings are getting 'lost' when they are being assigned to a variable in JavaScript.
The question is: What is the proper way to get the array of strings returned to a JavaScript variable?
I need to call into a Win32 API to get a series of strings, and I would like to return an array of those strings to JavaScript. This is for script that runs on local machine for administration scripts, not for the web browser.
My IDL file for the COM object has the interface that I am calling into as:
HRESULT GetArrayOfStrings([out, retval] SAFEARRAY(BSTR) * rgBstrStringArray);
The function returns correctly, but the strings are getting 'lost' when they are being assigned to a variable in JavaScript.
The question is: What is the proper way to get the array of strings returned to a JavaScript variable?
Share Improve this question edited Jun 25, 2017 at 9:36 ɢʀᴜɴᴛ 32.9k15 gold badges121 silver badges114 bronze badges asked Sep 5, 2008 at 3:07 Michael BennyMichael Benny 4283 silver badges8 bronze badges2 Answers
Reset to default 6If i recall correctly, you'll need to wrap the SAFEARRAY
in a VARIANT
in order for it to get through, and then use a VBArray object to unpack it on the JS side of things:
HRESULT GetArrayOfStrings(/*[out, retval]*/ VARIANT* pvarBstrStringArray)
{
// ...
_variant_t ret;
ret.vt = VT_ARRAY|VT_VARIANT;
ret.parray = rgBstrStringArray;
*pvarBstrStringArray = ret.Detach();
return S_OK;
}
then
var jsFriendlyStrings = new VBArray( axOb.GetArrayOfStrings() ).toArray();
Shog9 is correct. COM scripting requires that all outputs be VARIANTS.
In fact, it also requires that all the INPUTs be VARIANTS as well -- see the nasty details of IDispatch in your favorite help file. It's only thought the magic of the Dual Interface implementation by ATL and similar layers (which most likely is what you are using) that you don't have to worry about that. The input VARIANTs passed by the calling code are converted to match your method signature before your actual method is called.