1

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);
Citrus
  • 1,162
  • 1
  • 16
  • 38

2 Answers2

5

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")";
2501
  • 25,460
  • 4
  • 47
  • 87
1

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);
2501
  • 25,460
  • 4
  • 47
  • 87
FelipeDurar
  • 1,163
  • 9
  • 15
  • 1
    This will fail to compile because `__DATE__` by itself expands to a `char*` string. – Jonathan Potter Dec 22 '14 at 10:36
  • 1
    @JonathanPotter: are you sure it doesn't simply expand to a quoted *string*? – Jongware Dec 22 '14 at 10:48
  • 1
    @Jongware It works because: http://stackoverflow.com/questions/5179384/what-happens-with-adjacent-string-literal-concatenation-when-there-is-a-modifier However this is undefined, only c99 and newer versions defined this: https://www.securecoding.cert.org/confluence/display/seccode/STR10-C.+Do+not+concatenate+different+type+of+string+literals – 2501 Dec 22 '14 at 10:56
  • still it gets a concatenating mismatched strings error – Citrus Dec 22 '14 at 11:03
  • @JonathanPotter: okay, I got it. Until this precise moment I ... *assumed* ... it was just a Preprocessor thing, as I never had any reason to doubt that. – Jongware Dec 22 '14 at 11:10
  • 1
    @Jongware: That's what I mean. It expands to a narrow string, not a wide string. And you can't concat `L"foo" "bar"` without some macro magic. – Jonathan Potter Dec 22 '14 at 11:10
  • I though this was a C question. So my comments aren't correct for C++. Look at the question tags Felipe. – 2501 Dec 22 '14 at 11:18
  • I put a different answer below – FelipeDurar Dec 22 '14 at 11:29
  • 1
    error C2308: concatenating mismatched strings with your answer too – Citrus Dec 22 '14 at 11:43