Cast (const) char * to LPCWSTR

Markus Meskanen picture Markus Meskanen · Jul 24, 2013 · Viewed 15.6k times · Source

I'm trying to use FindWindow() from WinAPI, and I want to ask an input for window's title from the user:

char *input;
cout << "Window title: ";
cin >> input;

Pretty standard. Now then, how do I convert this to LPCWSTR for FindWindow()?

I've already tried the following: _T(input), TEXT(input), (LPCWSTR)input but none of them worked. I also tried using wchar_t instead of char, but I need char everywhere else so then I get dozens of errors for using wchar_t instead of char...

Answer

cdmh picture cdmh · Jul 24, 2013

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;