473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting between VARIANT and std::string

Hi

Can anyone show me how to convert between VARIANT and std::string and back,
thanks!

Torben

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.735 / Virus Database: 489 - Release Date: 06/08/2004
Nov 17 '05 #1
5 15882
Torben Laursen wrote:
Hi

Can anyone show me how to convert between VARIANT and std::string and
back, thanks!


Here's one way, not necessarily the best. In general, std::string doesn't
play very well with VARIANT.

#include <comutil.h>
#include <string>

std::string from_variant(VARIANT& vt)
{
_bstr_t bs(vt);
return std::string(static_cast<const char*>(bs));
}

void to_variant(const std::string& str, VARIANT& vt)
{
_bstr_t bs(str.c_str());
reinterpret_cast<_variant_t&>(vt) = bs;
}

-cd
Nov 17 '05 #2
Hi Carl

Thanks.
I get a exception when I try to use: to_variant(const std::string& str,
VARIANT& vt),
it throw's at reinterpret_cast. Do you know whats wrong, thanks!

Torben
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Torben Laursen wrote:
Hi

Can anyone show me how to convert between VARIANT and std::string and
back, thanks!


Here's one way, not necessarily the best. In general, std::string doesn't
play very well with VARIANT.

#include <comutil.h>
#include <string>

std::string from_variant(VARIANT& vt)
{
_bstr_t bs(vt);
return std::string(static_cast<const char*>(bs));
}

void to_variant(const std::string& str, VARIANT& vt)
{
_bstr_t bs(str.c_str());
reinterpret_cast<_variant_t&>(vt) = bs;
}

-cd

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.735 / Virus Database: 489 - Release Date: 06/08/2004
Nov 17 '05 #3
Torben Laursen wrote:
Hi Carl

Thanks.
I get a exception when I try to use: to_variant(const std::string&
str, VARIANT& vt),
it throw's at reinterpret_cast. Do you know whats wrong, thanks!


The following works correctly in my test:

// <Code>
#include <comutil.h>
#include <string>
#include <iostream>

std::string from_variant(VARIANT& vt)
{
_bstr_t bs(vt);
return std::string(static_cast<const char*>(bs));
}

void to_variant(const std::string& str, VARIANT& vt)
{
_bstr_t bs(str.c_str());
reinterpret_cast<_variant_t&>(vt) = bs;
}

int main()
{
std::string str("This is a test");
_variant_t vt;
to_variant(str,vt);
std::string str2 = from_variant(vt);
std::cout << "str: " << str << "\nstr2: " << str2 << "\n";
}

// </Code>
R:\>cl -GX -GR convert0817.cpp comsupp.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

convert0817.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:convert0817.exe
convert0817.obj
comsupp.lib

R:\>convert0817
str: This is a test
str2: This is a test
Nov 17 '05 #4
Torben Laursen wrote:
Hi Carl

Thanks.
I get a exception when I try to use: to_variant(const std::string&
str, VARIANT& vt),
it throw's at reinterpret_cast. Do you know whats wrong, thanks!


I believe that the _bstr_t(const variant_t&) constructor will throw an
exception if the variant contains a type that can't be converted to a
string. What's in the VARIANT that you're supplying?

-cd
Nov 17 '05 #5
Hi Carl

My test VARIANT was emty, that must have been the problem.

Thanks Torben

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uM*************@TK2MSFTNGP09.phx.gbl...
Torben Laursen wrote:
Hi Carl

Thanks.
I get a exception when I try to use: to_variant(const std::string&
str, VARIANT& vt),
it throw's at reinterpret_cast. Do you know whats wrong, thanks!


I believe that the _bstr_t(const variant_t&) constructor will throw an
exception if the variant contains a type that can't be converted to a
string. What's in the VARIANT that you're supplying?

-cd

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.735 / Virus Database: 489 - Release Date: 06/08/2004
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Victor Hannak | last post by:
I am taking a program written in Borland C++ Builder 4 and converting the non-GUI related code to be generic c++ that can run anywhere. My main issue at this point is dealing with the string...
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
32
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
1
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003)...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
4
by: Ramesh | last post by:
Hi, I need to convert unsigned long data to a std::string, I googled a bit to see if the string class supports any methods to achieve this - but didnt find any, I think of using sprintf and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.