就一个函数FormatMessage
#includevoid print_error(DWORD err){ //使用当前平台的字符集 _tsetlocale(LC_ALL, L""); //创建一块内存一直存放错误信息 static HANDLE g_heap = HeapCreate(0, DEFAULTSIZE, 0); static TCHAR *buf = (TCHAR*)HeapAlloc(g_heap, 0, DEFAULTSIZE); //使用当前平台的语言 DWORD syslocale = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT); DWORD ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, syslocale, buf, DEFAULTSIZE, NULL); if (!ret){ //如果上面没找到错误,去网络错误中查找 static HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES); if (hDll){ //如果在dll中查找,FORMAT_MESSAGE_FROM_HMODULE 添加上去, 第2个参数填写句柄 ret = FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, hDll, err, syslocale, buf, DEFAULTSIZE, NULL); } } if (ret && buf){ buf[ret] = 0; _tprintf(TEXT("buf:%s\n"), buf); } else{ _tprintf(TEXT("unknow error : %ld\n"), err); }}