View Full Version : How to extract an integer from the VARIANT structure?
I am trying serial port communications between two computers.
1.To send data,one computer runs a DOS-based program and use _outp() to send an integer.
2.To receive data,the other runs a windows-based program developed using MFC of VC,and MSComm is used to receive data.
Now I have trouble in extract the integer from the VARIANT structure after trying all bVal,iVal,dblVal,lVal....the retrieved data from these members are not correct.
Please give a hand to me.Thank you very much!
Fordy
07-24-2003, 03:46 AM
As you are in MFC, try the COleVariant wrapper or the VC++ version _variant_t.
Both have good extraction & conversion member functions
Bubba
07-24-2003, 09:48 AM
VARIANTs from VB do not always return correct values back to C. Data misalignment is probably the reason but I've not researched it enough to know. I simply re-coded the other app in C because I was getting a serious headache trying to pass data back and forth between C and VB.
COleVariant wrapper?
I have tried the COleVariant structure,the vt and iVal,bVal,lVal,dblVal...all these members doesn't work.
Would you tell me how to use COleVariant wrapper?
Yes,I just send a short integer from the DOS-based program using _outp().For example,I sent 2,3,4....But I failed to extract it from the received VARIANT structure of VC program.
Fordy
07-24-2003, 06:18 PM
Originally posted by onon
COleVariant wrapper?
I have tried the COleVariant structure,the vt and iVal,bVal,lVal,dblVal...all these members doesn't work.
Would you tell me how to use COleVariant wrapper?
I tend to use _variant_t more...but both are available with VC++ and both are pretty similar
#include <comdef.h>
void foo(VARIANT* lpVar)
{
_variant_t cVar(lpVar);
cVar.ChangeType(VT_I2);//assuming you want a 16 bit value
WORD wd = cVar;
//do whatever with wd
}
I have tried your code,there is a compiling error in the sentence:
WORD wd = cVar;
I also tried COleVariant instead of _variant_t,the similiar error occurs.
the error message is:
cannot convert from 'class COleVariant' to 'unsigned short'. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Fordy
07-25-2003, 03:45 AM
It should work with _variant_t as shown as that object has an operator unsigned short() as well as an operator short().
What compiler?
I use MS Visual C++ as the compiler.
the code cannot pass the compile because of the error I mentioned above.
Do you use the same compiler?
Fordy
07-25-2003, 11:03 AM
Originally posted by onon
I use MS Visual C++ as the compiler.
the code cannot pass the compile because of the error I mentioned above.
Do you use the same compiler?
I'm using Visual C++.NET. and it works on that, but suprise suprise.....I tried it on VC++6 (I assume you have this one) and it didnt compile.
I think version 6's implementation of _variant_t lacked an operator short() function (now added on VC++.NET).
This will work on VC++6
void foo(VARIANT* lpVar)
{
_variant_t cVar(lpVar);
cVar.ChangeType(VT_I2);//assuming you want a 16 bit value
SHORT wd = cVar;
//do whatever with wd
}
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.