Question

I want to use an instrument that has a COM object interface. Unfortunately there is no C++ example in the SDK, only C#.

After creating the wrapper class, I can successfully read the instrument's serial number into a VARIANT:

 VARIANT snumbers;
 PowerMeter=new TCoLMMeasurement(Form1);
 PowerMeter->ScanUSB(&snumbers);

However I am not able to get the serial number out properly from the VARIANT that has a (VT_ARRAY | VT_BSTR) type. I read the documentation of the type, here is what I tried and what I got:

info("type of data: " + AnsiString(snumbers.vt));
info("VT_ARRAY|VT_BSTR = " + AnsiString(VT_ARRAY |VT_BSTR));
info("Size of an element(bytes): " + AnsiString(snumbers.parray->cbElements));
info("Number of dimensions: " + AnsiString(snumbers.parray->cDims));
info("Size of dimension 0: " + AnsiString(snumbers.parray->rgsabound[0].cElements));
info("value of pVdata[0]: " + AnsiString(((long*)(snumbers.parray->pvData))[0]));
info(((long*)snumbers.bstrVal)[0]);

SAFEARRAY* pSafeArray=NULL;
pSafeArray=V_ARRAY(&snumbers);
long element=0;
long i=0;
SafeArrayGetElement(pSafeArray,&i,(void*)&element);
info(element);

The above code displays these results:

type of data: 8200
VT_ARRAY|VT_BSTR = 8200
Size of an element(bytes): 4
Number of dimensions: 1
Size of dimension 0: 1
value of pVdata[0]: 1591532
25165825
1592716

The serial number should be 700187.

I made the testint in a VCL forms application created with CodeGear RAD C++ Builder 2007

Thanks for any kind of help!

Gaszton

Was it helpful?

Solution

Using a BSTR conversion solved the problem. I was not aware of the BSTR type complexity.

info("value of pVdata[0]: " + AnsiString(((BSTR*)(snumbers.parray->pvData))[0]));

or

SAFEARRAY* pSafeArray=NULL;
pSafeArray=V_ARRAY(&snumbers);
BSTR element;
long i=0;
SafeArrayGetElement(pSafeArray,&i,(void*)&element);
info("value of element0: " + AnsiString(element));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top