How to return an array to javascript from activex control

Posted on 2006-04-21
Last Modified: 2011-10-03


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 ??;
Question by:gammae

    Author Comment


    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)

    VARIANT CVombaShotsCtrl::test()
        VARIANT 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;
        V_VT(&vaResult) = VT_ARRAY | VT_BSTR;
        V_ARRAY(&vaResult) = psa;    
        return vaResult;
    LVL 17

    Expert Comment

    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.

    Author Comment

    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.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now