473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert ^string to string or to LPCWSTR

Hi,

How can we convert string^ to String or to LPCWSTR ?
thx,
Maileen
Nov 17 '05 #1
3 3308
Maileen wrote:
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
Nov 17 '05 #2
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

Nov 17 '05 #3
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.

--
Regards,
Nish [VC++ MVP]
"Maileen" <no*****@nospam.com> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
9
by: Iwan Petrow | last post by:
Hi, I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm". How could I convert this string to DateTime type (I prefer somehow to set this as a pattern if it is possible because...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
2
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
2
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
4
by: ad | last post by:
I have a string variable. How can I convert the string to MemoryStream?
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
9
by: X Enterprises | last post by:
Hello :) I'm not new to C++, I just don't use it as much as I used to. But anyway, I need to convert LPSTR to LPCWSTR. Here's the function i'm using it in: MYCOMMAND void PrintText( LPSTR...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.