accessing an array of integers from jscript in a vc++ activex/com
Hi there,
I am new to using ActiveX and COM and as you can imagine, having a hard
time wading through these waters. Hope you will be able to help me.
I am trying to pass an array of integers from JScript to an ActiveX/COM
dll as well as back to the JScript. I am totally lost as to what I need
to be using to achieve this and how - should I be using Variant or
IDispatch, etc.
I have noticed that when I use IDispatch I am able to properly read the
values that I have hardcoded in JScript (eg. arrayElement[0] = 5 ... )
But when I try to populate the array with the values returned it doesnt
work. The values returned are of type: VARIANT | VT_BSTR.
Heres the code that Im using to return my values :
USES_CONVERSION; // For Conversion macros
HRESULT hReturn = S_OK; // Return value
SAFEARRAY *ptrSafeArray = NULL; // Safe array
SAFEARRAYBOUND safeArrayBounds[1]; // Safe array size
char ptrBuffer[MAX_STR_LEN]; // Buffer
// Fill the safe array bounds
safeArrayBounds[0].lLbound = 0; // Lower bound
_variant_t vOut;
// Number of elements
safeArrayBounds[0].cElements = 5;
//Create the safe array
ptrSafeArray= SafeArrayCreate(VT_VARIANT|VT_BSTR,1,safeArrayBounds);
//Populating the array
for(long loop = 0; loop < 5; loop++)
{
switch (loop)
{case 1:
sprintf(ptrBuffer,"%d",receivedStruct.address[0]);
break;
case 2:
sprintf(ptrBuffer,"%d",receivedStruct.address[1]);
break;
} // End index switch
// Put the element in safe array
vOut= A2W(ptrBuffer);
hReturn = SafeArrayPutElement(ptrSafeArray, &loop, &vOut)
}
// Fill the out parameter
ptrRetVal->vt = VT_VARIANT | VT_ARRAY| VT_BSTR;
ptrRetVal->parray = ptrSafeArray;
=======================
And at the JScript end I read this into a VBArray and then convert it
to a JScript array using VBArray.toArray()
Then when I try to populate the JScript integer array with the returned
values and pass it back to the DLL it doesnt seem to recognize them as
integers (eg. theAddress = Number(arrayFrmVB[0]))
Can you let me know how I should be reading integer arrays from JScript
in the DLL and passing them back once I am done processing them ??
Thanks