By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,492 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,492 IT Pros & Developers. It's quick & easy.

Converting between VARIANT and std::string

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.