473,782 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ 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 3348
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 = PtrToStringChar s(sm);
// PtrToStringChar s 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(Marsha l::StringToHGlo balAnsi(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 ::InteropServic es;
public ref class ScopedHGlobal
{
public:
ScopedHGlobal(I ntPtr p) : ptr(p) { }
~ScopedHGlobal( ) { Marshal::FreeHG lobal(ptr); }
char* c_str() { return reinterpret_cas t<char*>(ptr.To Pointer()); }
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 = PtrToStringChar s (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******** ******@TK2MSFTN GP10.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
10294
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
4
17686
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
9
2483
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 I have other possibilities as "dd~MM~yyyy, hh:mm")? Thanks.
15
10834
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
17175
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 compare both? Example:
2
7900
by: Neo | last post by:
how convert LPCSTR to LPCWSTR? regards, Mohammad Omer Nasir.
4
61126
by: ad | last post by:
I have a string variable. How can I convert the string to MemoryStream?
3
9201
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
442
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the String deciding...
9
36537
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 pString ) { if(pString)
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
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 we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.