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 / Languages / Managed C++ / November 2005

Tip: Looking for answers? Try searching our database.

convert ^string to string or to LPCWSTR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maileen - 03 Nov 2005 19:19 GMT
Hi,

How can we convert string^ to String or to LPCWSTR ?
thx,
Maileen
Tamas Demjen - 03 Nov 2005 19:22 GMT
> How can we convert string^ to String or to LPCWSTR ?

Unmanaged to Managed:
------------------------------------
char* su;
String^ sm = gcnew String(su);

wchar_t* su;
String^ sm = gcnew String(su);

std::string su;
String^ sm = gcnew String(su.c_str());

std::wstring su;
String^ sm = gcnew String(su.c_str());

Managed to Unmanaged:
------------------------------------
Wide string version:
   String^ sm = "Hello";
   pin_ptr<wchar_t> pu = PtrToStringChars(sm);
   // PtrToStringChars is an inline function in vcclr.h, and it returns
   // a raw pointer to the internal representation of the String.
   // After pinning "p", it can be passed to unmanaged code:
   wchar_t* su = pu;
   // when "pu" goes out of scope, "su" becomes invalid!

Ansi (8-bit) version:
   ScopedHGlobal s_handle(Marshal::StringToHGlobalAnsi(sm));
   char* su = s_handle.c_str();
   // when "s_handle" goes out of scope, "su" becomes invalid!
Where ScopedHGlobal is a helper class written by myself:
   using namespace System::Runtime::InteropServices;
   public ref class ScopedHGlobal
   {
   public:
      ScopedHGlobal(IntPtr p) : ptr(p) { }
      ~ScopedHGlobal() { Marshal::FreeHGlobal(ptr); }
      char* c_str() { return reinterpret_cast<char*>(ptr.ToPointer()); }
   private:
      System::IntPtr ptr;
   };

Tom
Brian Muth - 03 Nov 2005 19:29 GMT
You cannot convert String^ to String. It makes no sense.

To access the constant unicode string inside String^:

String^ x = gcnew String ("Hi.");
{
   pin_ptr<const wchar_t> pStr = PtrToStringChars (x);
   const wchar_t *s = pStr;
}    // unpin

Brian
Nishant Sivakumar - 03 Nov 2005 19:55 GMT
You cannot use stack semantics with String. So you can't have something like
:-

String s;

It always has to be :-

String^ s;

Stack semantics is also not available for array and delegate.

Signature

Regards,
Nish [VC++ MVP]

> Hi,
>
> How can we convert string^ to String or to LPCWSTR ?
> thx,
> Maileen

Rate this thread:







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.