469,312 Members | 2,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

String^ to LPCTSTR

How to convert String^ (managed) to LPCTSTR (unmanaged)?
Mar 5 '07 #1
3 10214
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:95**********************************@microsof t.com...
How to convert String^ (managed) to LPCTSTR (unmanaged)?
You can check on the usage of PtrToStringChars() here:

http://blogs.msdn.com/slippman/archi...02/147090.aspx

Regards,
Will
www.ivrforbeginners.com
Mar 5 '07 #2
Hi Joachim!
How to convert String^ (managed) to LPCTSTR (unmanaged)?
See: How to convert from System::String* to Char* in Visual C++ 2005 or
in Visual C++ .NET
http://support.microsoft.com/kb/311259/en-us

The "easysed to use" function is

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

{}
~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).T oPointer()))

{}
~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/
Mar 5 '07 #3
Hi Joachim!
How to convert String^ (managed) to LPCTSTR (unmanaged)?
Upps the last post was for managed C++... so here for C++/CLI:

#include <windows.h>
#include <tchar.h>
#include <string>
using namespace System;
struct StringConvA
{
char *szAnsi;
StringConvA(System::String ^s)
:
szAnsi(static_cast<char*>(System::Runtime::Interop Services::Marshal::String┬*ToHGlobalAnsi(s).ToPoin ter()))

{}
~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).T oPointer()))

{}
~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 = "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/
Mar 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by johnsto | last post: by
8 posts views Thread by Duncan Winn | last post: by
10 posts views Thread by farseer | last post: by
10 posts views Thread by Lucy Ludmiller | last post: by
14 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
14 posts views Thread by Mosfet | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.