vc++ - How to convert a CString into LPCWSTR

24,028

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.

Share:
24,028
prabhakaran
Author by

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, 2021

Comments

  • prabhakaran
    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
    Jörgen Sigvardsson about 13 years
    Better use the Cx2y classes instead of the old T-macros.
  • Jörgen Sigvardsson
    Jörgen Sigvardsson about 13 years
    Overly complex in MFC. Better use CStringW as suggested, or an appropriate Cx2y class.
  • IInspectable
    IInspectable almost 8 years
    A BSTR cannot be converted to an LPCWSTR. Both string types have different semantics. For example, nullptr is always a valid BSTR. LPCWSTR doesn't make any guarantees. Besides, a BSTR needs to be cleaned up calling SysFreeString. 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-in operator LPCTSTR() may be sufficient already. And really, unless you need a BSTR, don't just use one because you don't know what else to do. Sorry, -1.
  • IInspectable
    IInspectable almost 8 years
    CString has conversion c'tors. If you need a wide character string from an ANSI-encoded string, simply do CStringW wideString(narrowString);, where narrowString is of type CStringA.
  • IInspectable
    IInspectable almost 8 years
    So 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?