Returning a JS Array to JavaScript from a C++/JavaScript XPCOM object access through XPConnect

Cited from



void getArray(out unsigned long count, [array, size_is(count), retval] out long retv);

// C++  XPCOM

NS_IMETHODIMP MyComponent::GetArray(PRUint32* count, PRInt32** retv) {

    *count = 10;

    *retv = (PRInt32*)nsMemory::Alloc(*count * sizeof(PRInt32));

    for (int i = 0; i < 10; ++i) (*retv)[i] = i;

    return NS_OK;


// JavaScript  XPCOM

Component.prototype.getArray = function(count, retv) {

    count.value = 10; // You must assign the length of array to count.

    var array = new Array();


    return array;


// JavaScript Calling

var arr = myComponent.getArray({});

Blogged with Flock

9 Responses to “Returning a JS Array to JavaScript from a C++/JavaScript XPCOM object access through XPConnect”

  1. praveenmatanam Says:

    // XPIDL
    nsIArray getArray();

    // C++ XPCOM
    /* nsIArray getArray (); */
    NS_IMETHODIMP MyComponent::GetArray(nsIArray **_retval)
    unsigned int i = 0;

    unsigned int num = 5;

    nsresult rv = NS_OK;
    nsCOMPtr myMutableArray = do_CreateInstance(“;1”);

    for (i = 0; i < num; i++)
    char *name = “cString”;

    nsCOMPtr strArg;
    strArg = do_CreateInstance(“;1”, &rv);

    rv = myMutableArray->AppendElement(strArg, PR_FALSE);
    NS_ENSURE_SUCCESS(rv, rv);

    *_retval = myMutableArray;

    return NS_OK;

    // JavaScript Calling
    var arr = myComponent.getArray();
    var enumerate = arr.enumerate();

    while (enumerate.hasMoreElements())
    var name = enumerate.getNext().QueryInterface(Components.interfaces.nsISupportsCString).data;

  2. xpcomic Says:

    Hi all,

    First of all, thanks for the article , i really needed that
    kind of sample code and it worked smoothly!

    However i also need to pass a struct/interface to JS
    from C++ so i made the relevant changes based on this url:
    (see the TestIfaceTypeArrayOut sample)

    which compiled and linked fine but returned a runtime
    error at the end of the function call:

    (Access violation reading location 0xbaadf00d.).

    Thru debugging, i can see that the out param is getting
    correctly the value of the internal allocated memory which
    is 0xbaadf00d. I also made sure that i am allocating
    “count” elements. (returned size matches array size).

    I don’t understand why this is happening and I would
    really need some help… so If anyone faced the
    same problem or have an answer concerning this issue
    plz help !


  3. praveenmatanam Says:

    Try by adding reference count to the aResult like as follows.

  4. xpcomic Says:

    thx a lot, i managed to get it working !

  5. jojo Says:

    Thanks , I’ve tried the first example :, but when calling the fonction getarray() in jvascript I’ve got this error :
    “TypeError: Components.classes[cid] is undefined” ,
    is there a simple more recent sample, I want to return a byte array to javascript from C++

  6. jojo Says:

    sorry, the first method work finally,
    now I want to do the contrary return a bte array to c++ from javascript

  7. amol Says:

    Hi ,

    I am trying to access an array of strings(of size 12) from XPCom object.
    In JS, I am receiving correct length of array.However ,in JS array all 12 elements have same value (one of those allocated in cpp).
    I debugged my code in cpp and all 12 different strings are getting added correctly.AppendElement of NSMutableArray is used.I have used the code written above.

  8. Suyambu Says:

    I want to pass an array from Javascript to C++. Could you please help me with the solution.

    Thanks in advance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: