LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem converting Variant and IDispatch array into Safearray

Hi,
 
I am trying to convert an array of 6 VARIANTs and an array of 6 LPDISPATCH objects into Safearrays using the CA_Array1DToSafeArray. The return value for both functions is 0. When I try to read back the VARIANT and LPDISPATCH object values from the safearrays, only the first value in each of the arrays is returned. Following is the code I wrote :
 
HRESULT status;
CAObjHandle handleArray[6];
LPDISPATCH dispatchObj[6];
VARIANT variantValues[6];
 
LPSAFEARRAY lpdispatchSafeArray = NULL;
LPSAFEARRAY variantSafeArray = NULL;
 
VARIANT * variantValuesRead = NULL;
unsigned int noElements1;
VARIANT * dispatchObjRead = NULL;
unsigned int noElements2;
// Convert CAObjHandle to Dispatch pointer
status = CA_GetDispatchFromObjHandle (handleArray[0], &dispatchObj[0]);
status = CA_GetDispatchFromObjHandle (handleArray[1], &dispatchObj[1]);
status = CA_GetDispatchFromObjHandle (handleArray[2], &dispatchObj[2]);
status = CA_GetDispatchFromObjHandle (handleArray[3], &dispatchObj[3]);
status = CA_GetDispatchFromObjHandle (handleArray[4], &dispatchObj[4]);
status = CA_GetDispatchFromObjHandle (handleArray[5], &dispatchObj[5]);
status = CA_Array1DToSafeArray (dispatchObj, CAVT_DISPATCH, 6, &lpdispatchSafeArray);
status = CA_Array1DToSafeArray (variantValues, CAVT_VARIANT, 6, &variantSafeArray);
     
status = CA_SafeArrayTo1DArray (&lpdispatchSafeArray, CAVT_DISPATCH, &dispatchObjRead, &noElements1);
status = CA_SafeArrayTo1DArray (&variantSafeArray , CAVT_VARIANT, &variantValuesRead, &noElements2);
 
 
 
noElements1 and noElements2 return a value of 6. Is there something wrong in the way I am converting the arrays into Safearrays?
 
Thanks.
0 Kudos
Message 1 of 3
(5,263 Views)

Hi Swapna,

I am looking into this and will let you know when I find something.

Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 2 of 3
(5,231 Views)
Hi Swapna,
 
It looks like the VARIANT array part is working fine... I've modified your code slightly to initialize the array and to display the values of the resulting array.
Does this not work for you?
 
HRESULT status;
CAObjHandle handleArray[6];
VARIANT variantValues[6];
int i;
LPSAFEARRAY variantSafeArray = NULL;
VARIANT * variantValuesRead = NULL;
unsigned int noElements2;

for (i=0; i<6; i++)
          status = CA_VariantSetLong(&variantValues[i], i);
status = CA_Array1DToSafeArray (variantValues, CAVT_VARIANT, 6, &variantSafeArray);
status = CA_SafeArrayTo1DArray (&variantSafeArray , CAVT_VARIANT, &variantValuesRead, &noElements2);
for (i = 0; i
{
          status = CA_VariantGetLong (&variantValuesRead[i], &myCurrentValue); 
          printf("Value %d: %d\n", i, myCurrentValue);
}
getchar();
return;
 
One thing I wanted to mention is the watch window. If you add variantValuesRead to the watch window, by default the CVI engine will only display the first address of the array. This is because of the user protection code for the ActiveX library. The information displayed in the variables window is based on user protection information. This is actually expected behavior since VARIANT is not a C compiler type and so the array length estimation is now shown correctly in the variables and watch windows.
 
However, you can tell the IDE this by selecting Options >> Estimate Number of Elements and choosing 6. You must click on the variable first before selecting that menu item.
 
Hope this helps!
Jervin Justin
NI TestStand Product Manager
0 Kudos
Message 3 of 3
(5,197 Views)