Convert char * to LPWSTR
The std::mbstowcs
function is what you are looking for:
char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;
for string
s,
string text = "something";
wchar_t wtext[20];
mbstowcs(wtext, text.c_str(), text.length());//includes null
LPWSTR ptr = wtext;
--> ED: The "L" prefix only works on string literals, not variables. <--
Convert char[] to LPCWSTR
For this specific case, the fix is quite simple:
wchar_t szBuff[64];
swprintf(szBuff, L"%p", m_hWnd);
MessageBox(NULL, szBuff, L"Test print handler", MB_OK);
That is, use Unicode strings throughout. In general, when programming on Windows, using wchar_t
and UTF-16 is probably the simplest. It depends on how much interaction with other systems you have to do, of course.
For the general case, if you've got an ASCII (or char *
) string, use either WideCharToMultiByte for the general case, or mbstowcs
as @Matthew points out for simpler cases (mbstowcs
works if the string is in the current C locale).
How to convert char* to LPCWSTR?
Since cs
is a const char*
, cs[1]
is a const char
. C++ won't convert it to a pointer for you, because in most cases that doesn't make sense.
You could instead say &cs[1]
or cs+1
if the intent is to skip the first char. (That's what you're doing when you pass a pointer to the 1th element; in C++, indexes start at 0.) If the intent is to pass the whole string, then just pass cs
.
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
The Windows CreateFile function is actually a macro that expands to one of:
CreateFileA
, which takes a file path of typeconst char*
CreateFileW
, which takes a file path of typeconst wchar_t*
.
(The same is true for most of the functions in the Windows API that take a string.)
You're declaring the parameter const char* ComName
, but apparently compiling with UNICODE
defined, so it's calling the W
version of the function. There's no automatic conversion from const wchar_t*
to const char*
, hence the error.
Your options are to:
- Change the function parameter to a UTF-16 (
const wchar_t*
) string. - Keep the
char*
parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar. - Explicitly call
CreateFileA
instead ofCreateFile
. - Compile your program without
UNICODE
, so that the macros expand to theA
versions by default. - Kidnap a prominent Microsoft developer and force him to read UTF-8 Everywhere until he agrees to have Windows fully support UTF-8 as an “ANSI” code page, thus freeing Windows developers everywhere from this wide-character stuff.
Edit: I don't know if a kidnapping was involved, but Windows 10 1903 finally added support for UTF-8 as an ANSI code page.
LPCWSTR to char* and vice versa
The most probable reason that you had no problem before is that you turned on unicode in Visual C++ project's settings. See the accepted answer here to turn it off again (if it's possible for you):
How do I turn off Unicode in a VC++ project?
Otherwise you need to convert char *
to wchar_t *
using MultiByteToWideChar function.
Cast (const) char * to LPCWSTR
You can use the wide variants of cin and cout:
wchar_t input[256]; // don't really use a fixed size buffer!
wcout << L"Window title: ";
wcin >> input;
convert from char to LPCWSTR
LPCWSTR is expected to be the address of an array of wide chars (wchar_t
), and MessageBox()
expects that array to end in a null character.
You can then use an array with two elements, use the null character in the second one, and modify the first one like this
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
wchar_t myString[2];
myString[1] = '\0'; // Ensure the second element is the null char
switch (message)
{
case WM_CHAR:
myString[0] = LOWORD(wParam); // Modify the first element only
MessageBox(hWnd, myString, myString, MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Related Topics
Cmake: Include Library Dependencies in a Static Library
What Is Data Alignment? Why and When Should I Be Worried When Typecasting Pointers in C
Why Does Std::Map Operator[] Create an Object If the Key Doesn't Exist
Why Are Some Functions in <Cmath> Not in the Std Namespace
Console Output in a Qt Gui App
Significance of Parentheses in Decltype((C))
Openmp Set_Num_Threads() Is Not Working
Where Are C/C++ Main Function's Parameters
How to Return a Char Array from a Function
What's the Fastest Way to Pack 32 0/1 Values into the Bits of a Single 32-Bit Variable
How to Use Covariant Return Types with Smart Pointers
How to Return in Void Function