System::String^ to const WCHAR* ?
-
I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete
-
I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete
Fritzables wrote:
Can anyone point out where I am going wrong ?
Just a guess but you are not using the documentation, examples and samples found on msdn.microsoft.com.
led mike
-
Fritzables wrote:
Can anyone point out where I am going wrong ?
Just a guess but you are not using the documentation, examples and samples found on msdn.microsoft.com.
led mike
G'Day led mike, Well, ya could be right despite the fact I have tried a number of times creating different search strings in the hope I may find something that steers me in the right direction. I am pretty new to C++ as I have recently come across from using Borland's Delphi, so I will use that as my excuse. :) Pete
-
I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete
I hope this example helps: #include using namespace System; using namespace System::Runtime::InteropServices; int main(array ^args) { String ^gcString = L"Hello, World!"; IntPtr intptr = IntPtr::Zero; try { intptr = Marshal::StringToHGlobalUni(gcString); const wchar_t *hgString = static_cast(intptr.ToPointer()); std::wcout << hgString << std::endl; } catch (Exception ^e) { Console::WriteLine(e->Message); } finally { if (intptr != IntPtr::Zero) { Marshal::FreeHGlobal(intptr); } } return 0; } -- modified at 19:20 Wednesday 29th November, 2006
-
G'Day led mike, Well, ya could be right despite the fact I have tried a number of times creating different search strings in the hope I may find something that steers me in the right direction. I am pretty new to C++ as I have recently come across from using Borland's Delphi, so I will use that as my excuse. :) Pete
Fritzables wrote:
despite the fact I have tried a number of times creating different search strings
That is easy to believe. Keep at it though. Developing the "skill" to find information is a critical skill in our field. If this helps at all, I use Google to do my searching and when I expect that MSDN will contain the information I seek I put "MSDN" as the first keyword in the Google search phrase. I find that Google provides much better results than the MSDN search engine. But that's just me. :-D Anyway using your question as an example my Google search string would be
MSDN System.String convert WCHAR
Try it out.led mike
-
I am trying to write an app which requires me to convert a String^ to a WCHAR but not having much luck with a resolution. Can anyone point out where I am going wrong ? I'm using Visual Studio 2005 and it's a console application. Pete
I use this easy way, but it is not recommended for large strings.
String ^s = "Hello world"; CString str = s; WCHAR *w = str.GetBuffer(); WCHAR *myChar = new WCHAR[str.GetLength() +1]; memcpy(myChar,w,str.GetLength()*sizeof(WCHAR)); myChar[str.GetLength()] = '\0'; // Just display the WCHAR AfxMessageBox(myChar);
-
I use this easy way, but it is not recommended for large strings.
String ^s = "Hello world"; CString str = s; WCHAR *w = str.GetBuffer(); WCHAR *myChar = new WCHAR[str.GetLength() +1]; memcpy(myChar,w,str.GetLength()*sizeof(WCHAR)); myChar[str.GetLength()] = '\0'; // Just display the WCHAR AfxMessageBox(myChar);
Thanks all for the help on this one - much appreciated. Fritzables.