Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convert String ^ in char *

19 views
Skip to first unread message

nicolas...@motorola.com

unread,
Apr 21, 2006, 3:10:23 PM4/21/06
to
hi all,

i'm using this code to convert a String ^ in char *


String ^str = "string .net";
IntPtr p =
System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
LPCSTR str2 = reinterpret_cast<LPCSTR>(static_cast<void *>(p));
System::Runtime::InteropServices::Marshal::FreeHGlobal(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.

Jochen Kalmbach [MVP]

unread,
Apr 22, 2006, 1:18:27 AM4/22/06
to
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::InteropServices::Marshal::String茅oHGlobalAnsi(s).ToPointer()))

{}
~StringConvA()
{


System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(szAnsi));
}
operator LPCSTR() const
{
return szAnsi;
}
};


struct StringConvW
{
wchar_t *szUnicode;
StringConvW(System::String* s)
:
szUnicode(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::范tringToHGlobalUni(s).ToPointer()))

{}
~StringConvW()
{

System::Runtime::InteropServices::Marshal::FreeHGlobal(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/

nicolas...@motorola.com

unread,
Apr 22, 2006, 3:23:17 AM4/22/06
to
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

Marcus Heege

unread,
Apr 22, 2006, 3:54:42 AM4/22/06
to
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

<nicolas...@motorola.com> wrote in message
news:1145690597.8...@i39g2000cwa.googlegroups.com...

Vadym Stetsyak

unread,
Apr 22, 2006, 6:54:36 AM4/22/06
to
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

nicolas...@motorola.com

unread,
Apr 22, 2006, 6:58:14 AM4/22/06
to
>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.

Jochen Kalmbach [MVP]

unread,
Apr 22, 2006, 7:39:03 AM4/22/06
to
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...

Vadym Stetsyak

unread,
Apr 22, 2006, 9:23:16 AM4/22/06
to
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...
0 new messages