views:

1136

answers:

2

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 JScript. 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 correcty, but the strings are getting 'lost' when they are being assigned to a variable in JScript.

The question is: What is the proper way to get the array of strings returned to a JScript variable?

+4  A: 

If 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
Should the return ret.Detach() statement actually be> pvarBstrStringArray = ret.Detach();Thanks
Mark
Yes, thanks Mark!
Shog9
+1  A: 

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.

Euro Micelli