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).
Cannot convert CHAR to LPWSTR
Instead of your current code:
FILE *sortie;
char fichier[256];// <--- HERE s my char table
int main(int argc, char *argv[])
{
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
GetCurrentDirectory(256, fichier);
strcat(fichier, "\\fichierlog.txt");
do e.g.
auto main() -> int
{
//on masque
HWND hwnd = GetForegroundWindow();
ShowWindow(hwnd, SW_HIDE);
int i, lettre, result, lastresult, lastletter, compteur;
std::wstring fichier( MAX_PATH, L'\0' );// <--- HERE s my char table
const DWORD len = GetCurrentDirectory( fichier.size(), &fichier[0] );
if( len == 0 || len >= fichier.size() ) { throw std::runtime_error( "GetCurrentDirectory failed." ); }
fichier.resize( len );
fichier += L"/fichierlog.txt";
std::ifstream sortie( fichier );
This should fix three issues:
You're compiling as Unicode (probably a Visual Studio project), but the code is for the Windows ANSI API.
You're using a C++ compiler, but the code is low level C.
Too small buffer for maximum path length, and possible buffer overrun for the concatenation.
Note that the ifstream
constructor that accepts a wide string is a Microsoft extension. It will however be practically required for Windows C++ compilers by the file system addition to the standard library in C++17.
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.
E0513 a value of type const char * cannot be assigned to an entity of type LPCWSTR
The W
in LPCWSTR
stands for wide
.
You are passing the narrow characters, while you program is compiled as UNICODE.
You can either add a prefix L
to all you strings or use a _T()
macro.
For example:
windowclass.lpszClassName = L"CrystalWindow";
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. <--
Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
You have UNICODE
defined, so MessageBox
is expecting a wide string.
Cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'
Put a 'L' in front of the "example.bmp" string, e.g:
file = CreateFile(L"exmple.bmp",GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
This transforms "example.bmp" into a wide-character string.
argument of type char * is incompatible with parameter of type LPCWSTR
You're building with the UNICODE
macro defined, which means that all functions default to their wide-character equivalent. So when you call SetConsoleTitle
that's really a macro that expands to SetConsoleTitleW
.
A wide character has the type wchar_t
and is incompatible with char
.
You either have to explicitly call SetConsoleTitleA
, remove the definition of UNICODE
, or start using TCHAR
and related types and macros.
Related Topics
How to Get File Extension from String in C++
Error::Make_Unique Is Not a Member of 'Std'
What Is the Branch in the Destructor Reported by Gcov
Pros & Cons of Putting All Code in Header Files in C++
Problems Using Member Function as Custom Deleter with Std::Shared_Ptr
Advantages of Classes with Only Static Methods in C++
C++, Static VS. Namespace VS. Singleton
Literal Initialization for Const References
Win32: How to Hide 3Rd Party Windows in Taskbar by Hwnd
Can Lambda Functions Be Recursive
"Field Has Incomplete Type" Error
How to Use Multiple Versions of Gcc
What Is the Meaning of Template<> with Empty Angle Brackets in C++
Why Is There No 2-Byte Float and Does an Implementation Already Exist
Do I Have to Use Atomic<Bool> for "Exit" Bool Variable