How to return an array to javascript from activex control


Hi,

I'm trying to add a function to an activeX control that will return an array of int to my javascript.

Can anyone help me accomplish that? I believe I need to use SafeArray

Some of my code:

BEGIN_DISPATCH_MAP(CVombaShotsCtrl, COleControl)
       DISP_FUNCTION(CVombaShotsCtrl, "test", test, VT_VARIANT, VTS_NONE)
...

VARIANT CVombaShotsCtrl::test()
{

return ??;
}
gammaeAsked:
Who is Participating?
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0
 
gammaeAuthor Commented:

After much more fooling around, I can't accomplish anything. What I have now return an unknow data type in javascript:

BEGIN_DISPATCH_MAP(CVombaShotsCtrl, COleControl)
     DISP_FUNCTION(CVombaShotsCtrl, "test", test, VT_VARIANT , VTS_NONE)
END_DISPATCH_MAP()

VARIANT CVombaShotsCtrl::test()
{
    VARIANT vaResult;
    VariantInit(&vaResult);

    SAFEARRAYBOUND sabound[1];  
    SAFEARRAY FAR* psa = NULL;
    BSTR bstr = NULL;    
    HRESULT hr;
   
    // Create an array of size 100
    sabound[0].cElements = 100;
    sabound[0].lLbound = 0;    
    psa = SafeArrayCreate(VT_BSTR, 1, sabound);
   
    // Fill each array element with the same string, "contents"
    bstr = SysAllocString(OLESTR("contents"));
    for (long i=0; i<100; i++)
    {
        hr = SafeArrayPutElement(psa, &i, bstr);
        if (FAILED(hr))
            goto error;
    }  
    SysFreeString(bstr);  
    V_VT(&vaResult) = VT_ARRAY | VT_BSTR;
    V_ARRAY(&vaResult) = psa;    
    return vaResult;
}
0
 
rstaveleyCommented:
I'm not sure of my ground here, but since no one else has commented, I'll stick my neck out.

Doesn't SAFEARRAY give you a VBArray rather than a regular array? I think you have to create a new VBArray with the returned data in your JScript and use the toArray method to get a reference to an array.

NB: This is IE specific.
0
 
gammaeAuthor Commented:
I tried that, however javascript won't accept the return value as a vbarray.

The best I have been able to do is return a string and use javascript split to build my array.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.