471,066 Members | 936 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

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 3084
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by IamZadok | last post: by
7 posts views Thread by MilanB | last post: by
15 posts views Thread by Yifan | last post: by
12 posts views Thread by Brian Henry | last post: by
2 posts views Thread by Steve Kershaw | last post: by
5 posts views Thread by Zytan | last post: by
12 posts views Thread by Peter | last post: by
14 posts views Thread by rtillmore | last post: by
reply views Thread by leo001 | last post: by

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.