Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vc++ - How to convert a CString into LPCWSTR

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?

like image 602
prabhakaran Avatar asked Mar 11 '11 10:03

prabhakaran


People also ask

How do you convert CString to Lpcwstr?

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.

How do you convert CString to Lpcstr?

Solution 4. CString p; m_editbox->GetWindowText(p); CWND *c = FindWindow(NULL,p); Also the title is no lucky choice, because I think the problem is not the conversion from CString to LPCTSTR . The call to FindWindow() is correct, when it is inside a CWnd derived class.

How do you convert CString to float in MFC?

CString pi = "3.14"; return _ttof(pi); Reading a string value and parse/convert it to float allows you to locate the error when there is one. All you need is a help of a C Run-time function: strtod() or atof().

How do you convert int to CString in MFC?

Solution 3. CString MyString; int MyInt; MyString. Format(L"%d",MyInt); Another way is to use the std library's to_wstring[^], and then cast the result to CString.


5 Answers

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.

like image 151
Inverse Avatar answered Oct 08 '22 20:10

Inverse


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.

like image 37
Jörgen Sigvardsson Avatar answered Oct 08 '22 22:10

Jörgen Sigvardsson


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;
like image 38
DavidK Avatar answered Oct 08 '22 20:10

DavidK


LPCWSTR pstr;
CString cstrTemp;

...

pstr = cstrTemp.AllocSysString();

AllocSysString will return a BSTR type string, that can be converted to LPCWSTR directly.

like image 37
Elliot Chen Avatar answered Oct 08 '22 20:10

Elliot Chen


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.

like image 30
Asha Avatar answered Oct 08 '22 20:10

Asha