How to Convert Char* to Lpcwstr

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

 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 type const char*
  • CreateFileW, which takes a file path of type const 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:

  1. Change the function parameter to a UTF-16 (const wchar_t*) string.
  2. Keep the char* parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar.
  3. Explicitly call CreateFileA instead of CreateFile.
  4. Compile your program without UNICODE, so that the macros expand to the A versions by default.
  5. 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



Leave a reply



Submit