Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / September 2004

Tip: Looking for answers? Try searching our database.

How to convert String to WCHAR ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dane - 13 Sep 2004 23:45 GMT
How can I convert a String to a WCHAR array? Here is basically the code I'm
trying to execute (in C++) but the casting from Char* -> WCHAR* does not work:

//////////////////////////////////////////////////////
#include <windows.h>
#using <mscorlib.dll>
using namespace System;

int main(int argc, char *argv[]) {

   String *s = S"this is a string";
   Char cArr[] = s->ToCharArray(0,s->Length);
   WCHAR *wcArr = (WCHAR*)&cArr;

   return 0;
}
//////////////////////////////////////////////////////
Willy Denoyette [MVP] - 15 Sep 2004 10:31 GMT
- No need to copy a string to an array.
- You can't safely take an address of a managed object (String) and cast it
to an unmanaged pointer.
Here is what you should do.

#include <vcclr.h>
...
String *s = S"this is a string";
wchar_t __pin* wcArr = PtrToStringChars(s); // Pin the internal string
buffer and return the address
...

Willy.
PS. Please post C++ questions to: microsoft.public.dotnet.languages.vc

> How can I convert a String to a WCHAR array? Here is basically the code
> I'm
[quoted text clipped - 15 lines]
> }
> //////////////////////////////////////////////////////

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.