473,573 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string <--> System::String*

Hello, I am working on cleaning up some code that I inherited and was
wondering if there is anything wrong with my function. I am fairly
proficient in standard C++ but I am pretty new to the .NET managed C++.
It seems to work fine, but everyone knows that programs with errors can
still appear to "work fine" :)

I am working with VS .NET 2003; I am unable to upgrade to 2005 at this
time, so I cannot use the newer syntax or features.

This function takes a std::string and converts it to a System::String* .
Here is the original:

System::String*
MarshalStdToNet String(std::str ing& ss)
{
if (ss.empty())
return new System::String( S"");

System::IntPtr ptr(static_cast <System::IntPtr >(static_cast<v oid*>(const_cas t<char*>(ss.c_s tr()))));

System::String* ret(System::Run time::InteropSe rvices::Marshal ::PtrToStringAn si(ptr));

return ret;
}
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String( s.c_str());
}
Have I oversimplified? Or is all that casting business really needed?
Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven't started trying to
change this one yet:

void
MarshalNetToStd String(System:: String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

const char* chars = (const char*)(Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
os = chars;
Marshal::FreeHG lobal(IntPtr((v oid*)chars));
}

--
Marcus Kwok
Feb 14 '06 #1
24 17452
Hi Marcus!
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String( s.c_str());
}
Have I oversimplified?
No. That look麓s really good!

Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven't started trying to
change this one yet:

void
MarshalNetToStd String(System:: String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

const char* chars = (const char*)(Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
os = chars;
Marshal::FreeHG lobal(IntPtr((v oid*)chars));
}


It looks ok...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Feb 14 '06 #2
Jochen Kalmbach [MVP] <no************ ********@holzma .de> wrote:
Hi Marcus!
Here is my updated version:
// function name is just shorthand for what I will eventually call it

System::String*
std2gc(const std::string& s)
{
return new System::String( s.c_str());
}
Have I oversimplified?


No. That look?s really good!


OK, thanks!
Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven't started trying to
change this one yet:

void
MarshalNetToStd String(System:: String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

const char* chars = (const char*)(Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
os = chars;
Marshal::FreeHG lobal(IntPtr((v oid*)chars));
}


It looks ok...


Thanks, after looking around some more I found almost identical code in
MSDN, so I think this is the "right" way to do it, though maybe I'll
change it to return the std::string instead of passing an out-parameter
to the function.

--
Marcus Kwok
Feb 14 '06 #3
Marcus Kwok <ri******@gehen nom.net> wrote:
This function takes a std::string and converts it to a
System::String* .


you should also think about std::wstring conversions; potentially these
could be faster, as CLI String is unicode one which means there would't
be conversion in String constructor and marshaller, and you would't lose
data (anything beyond ASCII)
B.

Feb 15 '06 #4
Bronek Kozicki <br**@rubikon.p l> wrote:
Marcus Kwok <ri******@gehen nom.net> wrote:
This function takes a std::string and converts it to a
System::String* .


you should also think about std::wstring conversions; potentially these
could be faster, as CLI String is unicode one which means there would't
be conversion in String constructor and marshaller, and you would't lose
data (anything beyond ASCII)


Thanks for the idea. However, this code is in a GUI that just passes
the strings to a backend calculation engine, so conversion speed is not
an issue. Also, all of our data is plain ASCII so I don't need to
support Unicode.

--
Marcus Kwok
Feb 15 '06 #5
Marcus Kwok <ri******@gehen nom.net> wrote:
Also, going in the other direction, is this the proper way to convert a
managed System::String* into a std::string? I haven't started trying to
change this one yet:

void
MarshalNetToStd String(System:: String* s, std::string& os)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

const char* chars = (const char*)(Marshal: :StringToHGloba lAnsi(s)).ToPoi nter();
os = chars;
Marshal::FreeHG lobal(IntPtr((v oid*)chars));
}


Here is my new implementation. Personally I feel this is a little
cleaner since it eliminates a few casts and the interface is more
consistent with the other (snipped) function, but it is essentially the
same:

std::string
gc2std(System:: String* s)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

IntPtr ip = Marshal::String ToHGlobalAnsi(s );
std::string ss = static_cast<con st char*>(ip.ToPoi nter());
Marshal::FreeHG lobal(ip);
return ss;
}
--
Marcus Kwok
Feb 16 '06 #6
Marcus Kwok <ri******@gehen nom.net> wrote:
std::string
gc2std(System:: String* s)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

IntPtr ip = Marshal::String ToHGlobalAnsi(s );
std::string ss = static_cast<con st char*>(ip.ToPoi nter());
Marshal::FreeHG lobal(ip);
return ss;
}


nice, however I see slight exception safete problem here : std::string
constructor might throw std::bad_alloc (eg. due to high memory
fragmentation and/or very long string being constructed) and in such
situation Marshal::FreeHG lobal will not be called.
B.

Feb 17 '06 #7
Bronek Kozicki <br**@rubikon.p l> wrote:
Marcus Kwok <ri******@gehen nom.net> wrote:
std::string
gc2std(System:: String* s)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

IntPtr ip = Marshal::String ToHGlobalAnsi(s );
std::string ss = static_cast<con st char*>(ip.ToPoi nter());
Marshal::FreeHG lobal(ip);
return ss;
}


nice, however I see slight exception safete problem here : std::string
constructor might throw std::bad_alloc (eg. due to high memory
fragmentation and/or very long string being constructed) and in such
situation Marshal::FreeHG lobal will not be called.


Ahh, yes, good point. This is exactly the kind of feedback I was
looking for :) I need to work on improving my exception-safety
abilities.

So, is this exception-safe? Although I vaguely recall reading somewhere
that duplicating the clean-up code like this is considered bad style. I
guess this is why the original version used the output parameter
(reference) to save the string, but I wonder if the assignment operator
in the original could cause the same issue.
std::string
gc2std(System:: String* s)
{
using System::IntPtr;
using System::Runtime ::InteropServic es::Marshal;

IntPtr ip = Marshal::String ToHGlobalAnsi(s );

std::string ss;
try {
ss = static_cast<con st char*>(ip.ToPoi nter());
}
catch (std::bad_alloc &) {
Marshal::FreeHG lobal(ip);
throw;
}

Marshal::FreeHG lobal(ip);

return ss;
}

--
Marcus Kwok
Feb 17 '06 #8
Hi Marcus!
try {
ss = static_cast<con st char*>(ip.ToPoi nter());
}
catch (std::bad_alloc &) {
Marshal::FreeHG lobal(ip);
throw;
}

Marshal::FreeHG lobal(ip);


I suggest to use "try-finally" and remove the "using System::IntPtr; ":

String *s = S"abc";
using System::Runtime ::InteropServic es::Marshal;
IntPtr ip = Marshal::String ToHGlobalAnsi(s );
std::string ss;
try
{
ss = static_cast<con st char*>(ip.ToPoi nter());
}
__finally
{
Marshal::FreeHG lobal(ip);
}
return ss;

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Feb 17 '06 #9

Better would be:

void Conv(System::St ring *s, std::string &ss)
{
using System::Runtime ::InteropServic es::Marshal;
IntPtr ip = Marshal::String ToHGlobalAnsi(s );
ss.clear();
try
{
ss = static_cast<con st char*>(ip.ToPoi nter());
}
__finally
{
Marshal::FreeHG lobal(ip);
}
}

So the memory will only be allocated once...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Feb 17 '06 #10

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

Similar topics

2
2430
by: Ludis | last post by:
Hi all Im having a problem im my class "utils". Im using linux slackware 8.0 (old? yes: so old). i trying g++ (gnu c++ compiler) 3.1 and glib default of Slack 8.0. I cant migrate this server now, but i need to compile my class in this machine... See my class:
10
1493
by: dotnetchic | last post by:
In VS 2003, I try creating a new VC7 MFC dll. Accepting all defaults, before adding ANY code, I get compiler errors out the wazoo having to do with this CString template. I ran across the same problem in porting some code over and compiling with the /clr switch and I resolved that issue by modifying a section of code that was typecasting a...
1
2378
by: lvbing1981 | last post by:
求助 请问函数strtok()是如何工作的? 如果我要讲一个字符串根据某一个标志实现分段 除了使用strtok()还是否有其他更有效率的函数或方法? 谢谢 help!! how about the strtok() function? who can tell me how the fnction strtok to deal with the string?
3
6135
by: amirbehzadan | last post by:
Hello, I am writing some C++ classes and want to export them as .dll files so other users can import them and use the methods I have provided in those classes. I have two types of classes : unmanaged (or regular classes) and managed (__gc classes). I already know how to use "__declspec(dllexport)" to export and "__declspec(dllimport)" to...
0
2251
by: npotnis | last post by:
Hi All, I am trying to compile an existing C++ unamanaged application with the /clr switch in VS.Net 2003, as I need to use managed extensions in the code. In a cpp file I have the following code : CString strRet; COleVariant var = m_pRSTFormList->GetFieldValue("FormName"); if(var.vt == VT_BSTR) strRet = var.pbVal;
17
10882
by: B. Williams | last post by:
I am receiving two warnings in my code and can't figure out why. Will someone look at this and tell me what I have done wrong? #include <iostream> using std::cout; #include <string> using std::string;
3
1678
by: tonysonney | last post by:
i was programming an assembler as part of my assignment. In that to check errors in the program, i wanted to check whether the argument is an intiger or a character? Can any one help me how to do that? Thanks in advance
176
8285
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the...
3
1310
by: dragonslayer008 | last post by:
I need to expose a String property, so I have: property String^ Filename; in my ref class. Now, in a member function, I need to call a native function that takes a std::wstring parameter. Of course, the compiler is complaining of me trying to convert String^ to std::wstring. What is the solution?
8
2533
by: =?Utf-8?B?U3RldmUgQmVobWFu?= | last post by:
In Visual C++ 8.0 Express I am trying to programmatically change the contents of a text box. In form1.h I find the initializing code _PAYEE->Text=L"a";. I assumed that the type of "a" in this expression is const char (which may be a drastic error). Be that as it may I have tried every conceivable datatype on the right side of...
0
7679
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...
0
7996
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. ...
1
7760
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...
0
8049
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5573
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...
0
5284
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...
0
3724
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...
1
2185
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
0
1029
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...

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.