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?
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.
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.
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().
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.
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.
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.
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;
LPCWSTR pstr;
CString cstrTemp;
...
pstr = cstrTemp.AllocSysString();
AllocSysString will return a BSTR type string, that can be converted to LPCWSTR directly.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With