VC ++ - How to Convert CString to LPCWSTR

I tried to do this, but I did not find any method for this. I ask about this because I'm new to windows. I tried stl-strings, but visual studio 2008 - accumulates errors in stl-wstring processing. I will talk about this later in another question. Can anyone shed light on this problem now?

+3
source share
6 answers

Use the CT2CW conversion class, like this one FuncTakingLPCWSTR(CT2CW(cstring_var)). This is guaranteed to work either in Unicode or in MBCS builds.

Also, keep in mind that the string passed to the function may be temporary, so do not save it for future reference.

+6
source

- MFC String, :

http://msdn.microsoft.com/en-us/library/87zae4a3%28VS.80%29.aspx

, CString LPCWSTR CT2W(s).

- CStringA CStringW. ascii CString , UNICODE. , :

CString your_string = "blah"
CStringW wide_string = your_string;

.

+4

, , Unicode ( ):

CString str("Hello, world!");
CStringW strw(str);
LPCWSTR ptr = strw;
+2

If you have compiler flags defined UNICODE,_UNICODE, then a simple assignment should be performed. If you have defined _MBCS, you need to use the MultiByteToWideChar method .

+1
source
LPCWSTR pstr;
CString cstrTemp;

...

pstr = cstrTemp.AllocSysString();

AllocSysString returns a string of type BSTR, which can be directly converted to LPCWSTR.

+1
source

You can also use the T2W () macro to avoid writing multiple lines of MultiByteToWideChar code.

0
source

All Articles