Hi. If I linked you to this thread, it's because I saw you making a very common mistake with WinAPI and I wanted to correct you.
You are passing the wrong type of string to WinAPI functions. That is, you're doing something like one of these:
MessageBox(NULL, "This is wrong", NULL,MB_OK);
MessageBox(NULL, L"This is also wrong", NULL,MB_OK);
Yes, yes... I know... the code is compiling just fine and is working for you. But really, it's wrong. Trust me. The correct way is to do one of these:
MessageBoxW(NULL, L"This is right!" ,NULL,MB_OK);
MessageBoxA(NULL, "This is right!" ,NULL,MB_OK);
MessageBox (NULL, TEXT("This is right!") ,NULL,MB_OK);
WinAPI has some weirdness with how it treats strings due to its switch to Unicode several years back. What this means is there are now 3 different character types to use (and 3 different sets of functions to match):
strings use the 'W' form of functions like
strings use the 'A' form of functions like
strings use the 'normal' form of functions like
Plainly, if you are calling
you need to give it a TCHAR string.
a TCHAR string (it's a char string) and therefore is incorrect.
I personally recommend that when you are dealing with WinAPI, you try to use wchar_t strings (and therefore the 'W' form of all WinAPI functions and structs) for everything.
If you only have ASCII characters in strings, you can get away with the char / 'A' versions if you don't care about Unicode support.
I highly recommend avoiding TCHAR / 'normal' versions at all costs. I've gone on several rants on why TCHARs are awkward, error prone, and provide no benefit. Here is one such rant, if interested:
It's worth noting that while I use MessageBox as an example here... the concept applies to every
WinAPI function which accepts a string as a parameter... and every WinAPI struct which has a string as a member (ie: OPENFILENAMEW)
I realize that to code properly, you might have to break some tough habits. But please make the effort.
Thanks for reading.