Admin Production ni-theme
Current Publication

CA_VariantGet1DArray

LabWindows/CVI

CA_VariantGet1DArray

HRESULT CA_VariantGet1DArray (VARIANT *variant, unsigned int arrayType, void *array, size_t *numberOfElements);

Purpose

Converts a 1D safe array in a variant parameter into a dynamically allocated C-style array.

Upon success, CA_VariantGet1DArray frees the contents of the variant parameter and marks it as empty.

Use CA_FreeMemory to free the returned array.

Example Code

The following code shows you how to use CA_VariantGet1Darray:

double * dblArray = NULL;
VARIANT variant;
unsigned numElements;
int index;
/* Call an ActiveX function that returns a safe array in a Variant. */
.
.

.
/* Convert the safe array the variant contains into a C-style array. */
CA_VariantGet1DArray(&variant, CAVT_DOUBLE, &dblArray, &numElements);
for (index = 0; index < numElements; index++)

printf("%f", dblArray[index]);

/* Free the allocated array. */
CA_FreeMemory(dblArray);

Parameters

Input
Name Type Description
arrayType unsigned int Data type of the array that CA_VariantGet1DArray creates from the safe array.

arrayType must be the same as the type of the safe array except for the following cases:

  • Creating a C-style array that contains char* elements from a BSTR safe array.
  • Creating a C-style array that contains CAObjHandle elements from an LPDISPATCH safe array.
arrayType can contain any of the fundamental data types for variants, safe arrays, and properties except CAVT_EMPTY or CAVT_NULL.

CA_VariantGet1DArray ignores the CAVT_ARRAY modifier.

If you do not know the type of the array, you can call CA_VariantGetType and pass its return value as the arrayType.

When you no longer need the C-style array, call CA_FreeMemory to discard it.
Output
Name Type Description
variant VARIANT Pointer to a variant that contains a 1D safe array.

CA_VariantGet1DArray frees the contents of the variant contents and marks it as empty.
array void * C-style array that CA_VariantGet1DArray dynamically allocates. The type of the array must be the same as arrayType. Pass the address of an array pointer.

You can treat this array like any other 1D C array.

Call CA_FreeMemory on the array to discard it when it is no longer needed. If the array contains any of the data types in the following table, you must call the appropriate function to free each element when you finish using it.
Data Type Function to Free Each Element
char * CA_FreeMemory
CAObjHandle CA_DiscardObjHandle
BSTR CA_FreeBSTR
LPUNKNOWN array[i]->lpVtbl->Release()
LPDISPATCH array[i]->lpVtbl->Release()
VARIANT CA_VariantClear
numberOfElements size_t Number of elements in array. You can pass NULL for this parameter.

Return Value

Name Type Description
status HRESULT A value indicating whether an error occurred. Negative error codes indicate function failure.

Error codes are defined in CVIversion\include\cviauto.h and <Program Files>\National Instruments\Shared\MSDTRedistributables\SDKHeaderFiles\8.1\winerror.h. The LabWindows/CVI ActiveX Library explicitly returns error codes. Other error codes in winerror.h are generated by ActiveX servers and passed on to you by the LabWindows/CVI ActiveX Library.

You can use CA_GetAutomationErrorString to get the description of an error code or CA_DisplayErrorInfo to display the description of the error code.

Additional Information

Library: ActiveX Library

Include file: cviauto.h

LabWindows/CVI compatibility: LabWindows/CVI 5.0 and later