CreateWindowEx shows chinese gibberish instead of original text

1.4k views Asked by At

I have a problem. I wanted to document my tool development, so instead of mspaint-ing a date on top of screenshot, I wanted to make the window name carry the date and time data. But instead of the string I've got only chinese characters.

gibberish

Here is my code where I want to assign the string to CreateWindowEx():

char *wndName = "Asphyx V0.01 (Build Date: " __DATE__ " " __TIME__ ")\0";

hWnd = CreateWindowEx(NULL,
                      L"WindowClass",
                      (LPCWSTR)wndName,
                      WS_OVERLAPPEDWINDOW,
                      300,
                      300,
                      wr.right - wr.left,
                      wr.bottom - wr.top,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);

EDIT: Guys, I appreciate your answers, but all of them gives me this

Error 29 error C2308: concatenating mismatched strings  

and the only somewhat working stuff was a yet deleted answer, but it gave me this:

enter image description here

he used this code:

char title[256];
sprintf(title, "Asphyx V0.01 (Build Date:  %s - %s)", __DATE__, __TIME__);

hWnd = CreateWindowEx(NULL,
                      L"WindowClass",
                      title,
                      WS_OVERLAPPEDWINDOW,
                      300,
                      300,
                      wr.right - wr.left,
                      wr.bottom - wr.top,
                      NULL,
                      NULL,
                      hInstance,
                      NULL);
2

There are 2 answers

3
2501 On BEST ANSWER

According to the standard if one of the strings has an encoding prefix, the rest of the string that don't, will be treated as having the same prefix.

This is not the case with Visual Studio. It is a bug.

You need to use a wide string and prefix every string literal with L including the macros:

#define WSTR2( s ) L##s
#define WSTR( s ) WSTR2( s )

wchar_t *wndName = L"Asphyx V0.01" WSTR(__DATE__) L" " WSTR(__TIME__) L")";
9
FelipeDurar On

the problem is because you are using cast to convert char to LPCWSTR, replace

char *wndName = "Asphyx V0.01 (Build Date: " __DATE__ " " __TIME__ ")\0";

to

 wchar_t *wndName = L"Asphyx V0.01 (Build Date: " __DATE__ " " __TIME__ ")";

Now you don't need more of cast in second parameter of CreateWindowEx.

wchar_t *wndName = L"Asphyx V0.01 (Build Date: " __DATE__ " " __TIME__ ")";

hWnd = CreateWindowEx(NULL,
                  L"WindowClass",
                  wndName,
                  WS_OVERLAPPEDWINDOW,
                  300,
                  300,
                  wr.right - wr.left,
                  wr.bottom - wr.top,
                  NULL,
                  NULL,
                  hInstance,
                  NULL);