LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract data from this Variant?

Hi All,

I hope someone could help me in solving this  problem.

I have a VARIANT variable named "num". I know that it contains an array of strings. By using the command:

 type = CA_VariantGetType(&num);

I obtain type=4211, that should correspond to an array of BSTR.

However, if I add "num" to "watch expression", I get "value={VT_BSTR | VT_ARRAY}" and the bitwise OR is 8200.

Now my questions are:

1- is it possible to see this array directly from the watch expression?

2- how can I extract this array of strings from the VARIANT?

This is really becoming challenging for me...

Regards and thanks for the attention

 

Francesco

 

0 Kudos
Message 1 of 4
(4,196 Views)

Hi Francesco,

I think it's not possible to see the content of a variant as an array of string while debugging your application.

However, the function CA_VariantGetBSTR() should do the job of retrieving the BSTR from variant you have.

You can find this function following this path in LabWindows/CVI:

 

Libraries->ActiveX Library->Variant Related Functions->Retrieving Values From Variant

 

Hope this helps

L

0 Kudos
Message 2 of 4
(4,171 Views)

Hello Lukaba,

thanks for the help.

Will it work even with an array of BSTR?

Regards

 

Francesco

0 Kudos
Message 3 of 4
(4,167 Views)

You're right, maybe it won't work with an array of BSTR.

In this case I think we have to options since in LabWindows/CVI we have the possibility to store an array of BSTR as a pointer using the function CA_VariantSetBSTRPtr () but we can also store it as an array using the function CA_VariantSet1DArray ().

 

You can know which of them was used to create your variant by using the function CA_VariantHasPtr ()  that returns TRUE only if the variant was created using CA_VariantSetBSTRPtr () and you can use CA_VariantHasArray () that returns TRUE if the variant was created using the CA_VariantSet1DArray ().

 

Once you know how your variant was created you can convert it to the array of BSTR using the function CA_VariantGetBSTRPtr()  or CA_VariantGet1DArray().

 

I created a little project and tested this functions. 

As you can see, the code creates an array of BSTR strings and store them in two different variants using both the ways I explained and then convert the variant back to two array of strings (they are pointers actually) and take the first string of both pointer that can be seen into the watch-expression window.

Here you have the code:

 

 

size_t numberOfElements; BSTR *myNEWBSTRArray; BSTR *myNEWBSTRPointer; HRESULT variantHasPointer; HRESULT variantHasArray; unsigned int variantTypeBSTRPointer; unsigned int variantTypeBSTRArray; BSTR myBSTR[3]; VARIANT myVariantBSTRPointer; VARIANT myVariantBSTRArray; char *ptrString; char *arrayString; BSTR firstOfPtr, firstOfArray ; //Allocate Variants myVariantBSTRPointer = CA_VariantEmpty (); myVariantBSTRArray = CA_VariantEmpty (); //Create the array of BSTR strings myBSTR[0] = SysAllocString(L"I am a happy BSTR"); myBSTR[1] = SysAllocString(L"I am another happy BSTR"); myBSTR[2] = SysAllocString(L"I am one more happy BSTR"); //FIRST WAY: Convert the BSTR array to a BSTR pointer variant CA_VariantSetBSTRPtr (&myVariantBSTRPointer, myBSTR); //Get the variant type variantTypeBSTRPointer = CA_VariantGetType (&myVariantBSTRPointer); //this is true since the variant was created as BSTRPtr variantHasPointer = CA_VariantHasPtr (&myVariantBSTRPointer); // Retrieve the BSTR array as a new BSTR pointer CA_VariantGetBSTRPtr (&myVariantBSTRPointer, &myNEWBSTRPointer); //Take the first BSTR string stored in this array firstOfPtr = myNEWBSTRPointer[0]; //SECOND WAY: Convert the BSTR array to a 1D array of BSTR variant CA_VariantSet1DArray (&myVariantBSTRArray, CAVT_BSTR, 3, myBSTR); //Get the variant type variantTypeBSTRArray = CA_VariantGetType (&myVariantBSTRArray); //this is true because this second variant was created as 1DArray variantHasArray = CA_VariantHasArray (&myVariantBSTRArray); //Retrieve the BSTR array as 1DArray. //NOTE: myNEWBSTRArray is a BSTR* just like myNEWBSTRPointer variable was. CA_VariantGet1DArray (&myVariantBSTRArray, CAVT_BSTR, &myNEWBSTRArray, &numberOfElements); //Take the first BSTR string stored in this array firstOfArray = myNEWBSTRArray[0]; //Convert the two BSTR strings to C-Strings that can be seen into the watch-expression CA_BSTRGetCString (firstOfArray, &arrayString); CA_BSTRGetCString (firstOfPtr, &ptrString);

 Let me know if it helps

 Best Regards

 

Luca Gallo

Application Engineer

National Instruments Italy

 

0 Kudos
Message 4 of 4
(4,135 Views)