473,947 Members | 12,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15926
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(VA RIANT& vt)
{
_bstr_t bs(vt);
return std::string(sta tic_cast<const char*>(bs));
}

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

-cd
Nov 17 '05 #2
Hi Carl

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

Torben
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:%2******** ********@TK2MSF TNGP11.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(VA RIANT& vt)
{
_bstr_t bs(vt);
return std::string(sta tic_cast<const char*>(bs));
}

void to_variant(cons t std::string& str, VARIANT& vt)
{
_bstr_t bs(str.c_str()) ;
reinterpret_cas t<_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(cons t std::string&
str, VARIANT& vt),
it throw's at reinterpret_cas t. 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(VA RIANT& vt)
{
_bstr_t bs(vt);
return std::string(sta tic_cast<const char*>(bs));
}

void to_variant(cons t std::string& str, VARIANT& vt)
{
_bstr_t bs(str.c_str()) ;
reinterpret_cas t<_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(cons t std::string&
str, VARIANT& vt),
it throw's at reinterpret_cas t. 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.nos pam>
wrote in message news:uM******** *****@TK2MSFTNG P09.phx.gbl...
Torben Laursen wrote:
Hi Carl

Thanks.
I get a exception when I try to use: to_variant(cons t std::string&
str, VARIANT& vt),
it throw's at reinterpret_cas t. 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
13117
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 classes used in this program. All strings in this program are of the Borland AnsiString class. I would like to convert them over to use std::string. In order to keep from breaking the original program, I was hoping to use std::string everywhere...
9
3311
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
49760
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(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character. To address this problem the GTKmm bindings for the GTK+ toolkit have implemented a own string class Glib::ustring...
1
3777
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) class library and type in the following code below: #include <map> #include<string>
2
10993
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 think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
84
15991
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
24307
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 MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
4
6523
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 converting to a char buf and then putting it back in std::string - is anyone aware of any other way? Thanks
0
9982
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11343
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6113
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3541
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.