本文中,企图通过对一段代码实例的 Debug 过程,介绍 Windows 编程中的常见字符编码问题的解决办法。
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define INFO_BUFFER_SIZE 32767
void printError(TCHAR* msg);
void main()
{
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
// Get and display the system directory.
if (!GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE))
printError(TEXT("GetSystemDirectory"));
_tprintf(TEXT("\nSystem Directory: %s"), infoBuf);
}
void printError(TCHAR* msg)
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;
eNum = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
sysMsg, 256, NULL);
// Trim the end of the line and terminate it with a null
p = sysMsg;
while ((*p > 31) || (*p == 9