By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

convert String ^ in char *

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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.