Quote:Original post by stenny
@Dhm
Thanks, that worked:
// some where in the declarationsconst char g_szClass[] = "Class";// somewhere in int WINAPI WinMain(...)wcex.lpszClassName = L" (g_szClass) ";
Uh...
Now, it don't work. the content of g_szClass[] is not automagically copied into the brand new string (L"(g_szClass)"). This evaluate to a string which contains "(g_szClass)" in unicode.
Quote:Original post by stenny
@EasilyConfused
That worked too, yours is even better. But what if I don't declare the char at the creation, like:
void AppError(BOOL Fatal, char *Text, ...){ TCHAR *CaptionText[12]; TCHAR *ErrorText[2048]; va_list valist; // build the messagebox caption based on fatal flag // if (Fatal == FALSE) { strcpy(CaptionText, "Error"); } else { strcpy(CaptionText, "Fatal Error"); } // build the variable text buffer // va_start(valist, Text); vsprintf(ErrorText, Text, valist); va_end(valist); // Display the messagebox // MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONERROR); // Post a quit message if error was fatal if (Fatal == TRUE) { PostQuitMessage(0); }}
This is the complete function
To work with TCHAR strings, you have to use TCHAR* functions, not the char* C functions. For example, strcpy() is now _tcscpy(). Your code becomes:
void AppError(BOOL Fatal, TCHAR *Text, ...){ // be aware that using such kind of code makes your program // vulnerable to buffer overflow attacks. Moreover, it allocates // a very big area on the stack (in UNICODE, 4120 bytes) TCHAR CaptionText[12]; // why did you declared an array of string? TCHAR ErrorText[2048]; // same question va_list valist; // build the messagebox caption based on fatal flag // if (Fatal == FALSE) { _tcscpy(CaptionText, "Error"); } else { _tcscpy(CaptionText, "Fatal Error"); } // build the variable text buffer // va_start(valist, Text); _vstprintf(ErrorText, Text, valist); va_end(valist); // Display the messagebox // MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONERROR); // Post a quit message if error was fatal if (Fatal == TRUE) { PostQuitMessage(0); }}
You also have another alternative: go to your project properties, and select the proper characater set (multi-byte vs. unicode). This will undefine the UNICODE macro in your code, meaning that you'll get rid of these L, TEXT() and wide char string stuff. Of course, using TCHAR would still be a must, because it will allow you to do a simple recompile to build eitehr the unicode version of your program or the non-unicode version.
HTH,