12-02-2005 11:06 AM
12-05-2005 05:53 PM
Here is one way to deal with variants containing strings:
#include <ansi_c.h>
#include <cviauto.h>
void main(void)
{
VARIANT vt;
char *inputArray[] = { "1", "23", "456" }, **outputArray = 0;
unsigned int vtType, numElems, numDims, i;
CA_VariantSet1DArray(&vt, CAVT_CSTRING, sizeof(inputArray)/sizeof(*inputArray), inputArray);
vtType = CA_VariantGetType(&vt);
CA_VariantGetArrayNumDims(&vt, &numDims);
CA_VariantGet1DArraySize(&vt, &numElems);
printf("Variant type: 0x%x, dimensions: %d, number of elements: %d\n", vtType, numDims, numElems);
if (vtType == (CAVT_CSTRING | CAVT_ARRAY))
{
CA_VariantGet1DArray(&vt, CAVT_CSTRING, &outputArray, &numElems);
for (i = 0; i < numElems; ++i)
{
printf("array[%d] = %s\n", i, outputArray[i]);
CA_FreeMemory(outputArray[i]);
}
CA_FreeMemory(outputArray);
}
CA_VariantClear(&vt);
getchar();
}
12-06-2005 11:11 PM
Hi Mohan,
I got everything to work with your help! The solution actually involved two separate parts. First, I got sloppy and forgot to initialize my char ** before passing it to CA_VariantGet1DArray(). Your sample code reminded me of this. No error code was ever returned (which is odd), but the array of strings was junk because the variant safearray from GetOPCServers() was junk, so who cares? Now to the other part of the solution.
In preparation for CVI 8, I completely uninstalled all NI software last night. I reloaded everything and then let CVI 8 regenerate the OPC automation wrapper. Suddenly, CA_VariantGet1DArraySize() started returning 7 elements--which is now the number of registered OPC servers on my computer. My CVI 7.1.1 install was reporting 0 elements from the same variant (at the time it should have been 5). So the big questions is: does CVI 8 have new ActiveX library\controller wizard code that was not in CVI 7.1.1 and this fixed the problem of a corrupt variant, or was it that a fresh reload of CVI 7.1.1 would have also caused the number of elements to be reported correctly? I would definitely like to hear an answer from NI on this.
/* begin whining */ My previous post indicated that the variables debug window and CA_VariantGetType() did not agree precisely on the variant data type. It still doesn't with CVI 8. Obviously, the variables window doesn't use the same code that CA_VariantGetType() does when it displays the variant value--which is also odd. If the variable window is more correct, then CA_VariantGetType() should have returned 0x106C instead of 0x1073. If CA_VariantGetType() is more correct, then the variable window should have indicated {CAVT_CSTRING | CAVT_ARRAY} instead of {CAVT_BSTR | CAVT_ARRAY}. It does change the CVI code to handle the BSTR, even though you can eventually arrive at the same char ** result. It just seems a bit sloppy to me. /* end whining */
As far as NI knows, is BSTR/CSTRING the only automation type pair that will involve a disagreement between the two sources? I would also like an answer from NI on this, please.
Orlan
12-12-2005 01:34 PM
12-12-2005 02:30 PM
Hey Bilal,
That explanation sounds good enough for me. I simply need to understand it as much as possible so that I don't do unnecessary coding. Also, I don't want to make stupid programming mistakes unnecessarily just because I don't understand the main concepts with a technology that is useful. Thanx again guys!
Orlan