Search code examples
javascriptcomactivex

How do I return an array of strings from an ActiveX object to JScript


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? ­­­­­­­­­­­­­­­­­­­­­­­­


Solution

  • 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();