473,714 Members | 2,539 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 15911
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
13087
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
3300
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
49710
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
3745
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
10916
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
15875
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
24272
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
6503
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
8797
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8704
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
9309
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...
1
9071
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,...
1
6629
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4462
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
4717
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.