none
how to convert string to variant in vc++ 6.0

    Question

  • Hello all,

    How to convert a string to variant in vc++ 6.0 . How to retrieve a VT_I4 value from variant.

     Please help me .

    Thursday, July 02, 2009 6:04 AM

Answers

  • Hello Rajesh,

    You can use COleVariant . For instance,

    // std::string to variant.
    std::string strMessage = "Rabbit";
    COleVariant objVariant( strMessage.c_str() );
    VARIANT variant = objVariant.Detach();
    
    // CString to variant.
    CString csMessage = "Rabbit";
    COleVariant objVariant1( csMessage );
    VARIANT variant1 = objVariant.Detach();
    

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 6:38 AM
  • Use SysAllocString() to create the BSTR you need.  VariantChangeType() to convert to VT_I4.  The _variant_t helper class from comutil.h can be very helpful.

    Hans Passant.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 10:35 AM
    Moderator
  • Hi Rajesh,

    I'll strongly recommend that you read this entire two part article to get a complete understanding of this topic. The first part explains you the very basics of strings and then explains the basics of character sets available. The second part explains about C-Style strings and string wrapper classes available on Windows. It additionally explains the manipulation of data and discusses the gotchas involved in conversion of one string data type to another.

    Part 1: http://www.codeproject.com/KB/string/cppstringguide1.aspx
    Part 2: http://www.codeproject.com/KB/string/cppstringguide2.aspx
    Best regards, Rajesh R Subramanian.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 11:00 AM

All replies

  • Hello Rajesh,

    You can use COleVariant . For instance,

    // std::string to variant.
    std::string strMessage = "Rabbit";
    COleVariant objVariant( strMessage.c_str() );
    VARIANT variant = objVariant.Detach();
    
    // CString to variant.
    CString csMessage = "Rabbit";
    COleVariant objVariant1( csMessage );
    VARIANT variant1 = objVariant.Detach();
    

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 6:38 AM
  • Use SysAllocString() to create the BSTR you need.  VariantChangeType() to convert to VT_I4.  The _variant_t helper class from comutil.h can be very helpful.

    Hans Passant.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 10:35 AM
    Moderator
  • Hi Rajesh,

    I'll strongly recommend that you read this entire two part article to get a complete understanding of this topic. The first part explains you the very basics of strings and then explains the basics of character sets available. The second part explains about C-Style strings and string wrapper classes available on Windows. It additionally explains the manipulation of data and discusses the gotchas involved in conversion of one string data type to another.

    Part 1: http://www.codeproject.com/KB/string/cppstringguide1.aspx
    Part 2: http://www.codeproject.com/KB/string/cppstringguide2.aspx
    Best regards, Rajesh R Subramanian.
    • Marked as answer by Nancy Shao Thursday, July 09, 2009 5:19 AM
    Thursday, July 02, 2009 11:00 AM
  • thanx rajesh,jijoraj.


    After coverting strring to variant i just need extract the field VT_I4 from variant right ? so that i can return that field from variant.
    Thursday, July 02, 2009 11:17 AM
  • VariantChangeType() to convert to VT_I4.


    Hans Passant.
    Thursday, July 02, 2009 12:21 PM
    Moderator