472,122 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

String^ to LPCTSTR

How to convert String^ (managed) to LPCTSTR (unmanaged)?
Mar 5 '07 #1
3 10341
"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
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.