Thread: How to extract an integer from the VARIANT structure?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    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!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As you are in MFC, try the COleVariant wrapper or the VC++ version _variant_t.

    Both have good extraction & conversion member functions

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    5
    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?

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    it is a short integer

    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.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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

    Code:
    #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
    }

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    Thanks,but it still exists some problem

    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

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It should work with _variant_t as shown as that object has an operator unsigned short() as well as an operator short().

    What compiler?

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    the compiler is MS Visual C++

    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?

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: the compiler is MS Visual C++

    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

    Code:
    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
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  5. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM