473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert String ^ in char *

hi all,

i'm using this code to convert a String ^ in char *
String ^str = "string .net";
IntPtr p =
System::Runtime::InteropServices::Marshal::StringT oHGlobalAnsi(str);
LPCSTR str2 = reinterpret_cast<LPCSTR>(static_cast<void *>(p));
System::Runtime::InteropServices::Marshal::FreeHGl obal(p);
I would like to make safe code, so i would like to use safe_cast
instead of reinterpret_cast or static_cast.

I tryed, but i can't achieve making something to work.

Do you have an idea ?

Thanks in advance for your help
Nicolas H.

Apr 21 '06 #1
7 3230
Hi nicolas!
i'm using this code to convert a String ^ in char *


I use the following struct and code:

struct StringConvA
{
char *szAnsi;
StringConvA(System::String* s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::String*ToHGlobalAnsi(s).ToPoint er()))

{}
~StringConvA()
{
System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};
struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String* s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::I nteropServices::Marshal::*StringToHGlobalUni(s).To Pointer()))

{}
~StringConvW()
{

System::Runtime::InteropServices::Marshal::FreeHGl obal(IntPtr(szUnicode));
}
operator LPCWSTR() const
{
return szUnicode;
}
};
#ifdef _UNICODE
#define StringConvT StringConvW
#else
#define StringConvT StringConvA
#endif

int _tmain()
{
String *s = S"abc";
std::string ansi = StringConvA(s);
std::wstring unicode = StringConvW(s);
_tprintf(_T("%s"), (LPCTSTR) StringConvT(s));
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Apr 22 '06 #2
thanks for your answer Jochen

Your method looks like mine, using static_cast.

I would rather user safe_cast, but this should not be possible ...
Regards,
Nicolas

Apr 22 '06 #3
What would the safe_cast give you?

If you compile with /clr:safe, you can not use pointers at all. If you use
/clr:pure or /clr, and encapsulate the ugly cast in a separate class that
also provides cleanup of the native data, you should be fine.

Marcus

<ni*************@motorola.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
thanks for your answer Jochen

Your method looks like mine, using static_cast.

I would rather user safe_cast, but this should not be possible ...
Regards,
Nicolas

Apr 22 '06 #4
Hello, Jochen!

The same can also be accomplished, using
pin_ptr class and PtrToStringChars(...) inline func.

String^ managedString;
pin_ptr<const TCHAR> srvNameUnmanged;
srvNameUnmanged = PtrToStringChars(managedString);

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Apr 22 '06 #5
>What would the safe_cast give you?
If you compile with /clr:safe, you can not use pointers at all. If you use
/clr:pure or /clr, and encapsulate the ugly cast in a separate class that
also provides cleanup of the native data, you should be fine. Marcus


Yes, you're right Marcus, that's the good question :)
No need to make it safe or to get error messages exceptions

Thanks for your help

Nicolas H.

Apr 22 '06 #6
Vadym Stetsyak schrieb:
Hello, Jochen!

The same can also be accomplished, using
pin_ptr class and PtrToStringChars(...) inline func.

String^ managedString;
pin_ptr<const TCHAR> srvNameUnmanged;
srvNameUnmanged = PtrToStringChars(managedString);


Yes, at least for the W-Version... for the A-Version you need to use
such a construct...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Apr 22 '06 #7
JKM> Yes, at least for the W-Version... for the A-Version you need to use
JKM> such a construct...

Agree, this constuct is valid only for W, since String^ are always unicodes...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Apr 22 '06 #8

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

Similar topics

27
51645
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
5
6142
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
7
66521
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
10796
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
12
2944
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
2
2336
by: Steve Kershaw | last post by:
I can convert from a string to a char array (char) by using string.ToCharArray() function. Now how do I convert it back? The string looks like this: "000457". I've converted it to a char array:...
5
38507
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
4
17323
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
13507
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
14
13426
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
7203
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
7087
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...
0
7334
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...
1
6993
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...
0
7462
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
5579
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
4675
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...
0
1514
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 ...
1
737
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.