vc++ - How to convert a CString into LPCWSTR
Solution 1
The easiest way is to use the MFC String Conversion Macros, defined at:
https://docs.microsoft.com/en-us/cpp/atl/reference/string-conversion-macros?view=msvc-160
For example, the macro to convert CString
to LPCWSTR
is CT2W(s)
.
Another way is to use the specialized CStringA
and CStringW
classes. These are the corresponding ascii and wide versions of CString
depending on if you're compile with the UNICODE
flag. So you can use:
CString your_string = "blah"
CStringW wide_string = your_string;
to get a wide version of your string.
Solution 2
Use the conversion class CT2CW like this FuncTakingLPCWSTR(CT2CW(cstring_var))
. This is guaranteed to work in either Unicode or MBCS builds.
Also, keep in mind that the string passed to the function may be a temporary, so don't store it for later use.
Solution 3
This should do it, assuming your application isn't already set to Unicode (if it is, just cast directly):
CString str("Hello, world!");
CStringW strw(str);
LPCWSTR ptr = strw;
Solution 4
LPCWSTR pstr;
CString cstrTemp;
...
pstr = cstrTemp.AllocSysString();
AllocSysString will return a BSTR type string, that can be converted to LPCWSTR directly.
Solution 5
If you have UNICODE,_UNICODE
compiler flags defined then a simple assignment should work. If you have _MBCS
defined you need to use MultiByteToWideChar method.
prabhakaran
A newbie to qt,openGL,NetworkProgramming Know python,perl,shell scripting. A well versed(medium level) C++,linux,perl,c# programmer Crawling around javascript,xul and new technologies. I am a Linux lover got tricked into windows.
Updated on January 18, 2021Comments
-
prabhakaran over 3 years
I tried to do this , but I didn't find any method for this. I am asking this due to the fact that I am new to windows. I tried stl-strings, but visual studio 2008- accumulates bugs in stl-wstring-handling. I will say a lot about that thing later, in other question. Now Can anybody shed light on this issue?
-
Jörgen Sigvardsson about 13 yearsBetter use the Cx2y classes instead of the old T-macros.
-
Jörgen Sigvardsson about 13 yearsOverly complex in MFC. Better use CStringW as suggested, or an appropriate Cx2y class.
-
IInspectable almost 8 yearsA
BSTR
cannot be converted to anLPCWSTR
. Both string types have different semantics. For example,nullptr
is always a validBSTR
.LPCWSTR
doesn't make any guarantees. Besides, aBSTR
needs to be cleaned up callingSysFreeString
. Your code doesn't show this, and your explanation doesn't note this either. Since the OP is asking for a const pointer, chances are they do not even need a copy. In that case, the built-inoperator LPCTSTR()
may be sufficient already. And really, unless you need aBSTR
, don't just use one because you don't know what else to do. Sorry, -1. -
IInspectable almost 8 years
CString
has conversion c'tors. If you need a wide character string from an ANSI-encoded string, simply doCStringW wideString(narrowString);
, wherenarrowString
is of typeCStringA
. -
IInspectable almost 8 yearsSo many convoluted (and right-out wrong) answers, and the only correct way to do this in MFC (through conversion c'tors) didn't get any upvotes?