// 功能:这是一个简单的GetLastError 从数字ID转化成 文本的演示。。
// 参考《Windows 核心编程》第一章 2014-11-02
#include "stdafx.h"
#include<iostream>
#include <Windows.h>
void ShowLastErrorMessage(DWORD dwError)
{
HLOCAL hlocal = NULL; // Buffer that gets the error message string
// Get the error code's textual description
BOOL fOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |\
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), //MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(PTSTR)&hlocal,
0,
NULL);
if (!fOk)
{
// Is it a network-related error?
HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL,
DONT_RESOLVE_DLL_REFERENCES);
if (hDll != NULL)
{
FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM |\
FORMAT_MESSAGE_IGNORE_INSERTS,
hDll,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), //MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(PTSTR)&hlocal,
0,
NULL);
FreeLibrary(hDll);
}
}
if (hlocal != NULL)
{
//::MessageBox(NULL, (LPCSTR)LocalLock(hlocal), TEXT(""), MB_OK);
std::cout << (LPCSTR)LocalLock(hlocal) << std::endl;
LocalFree(hlocal);
}
else
{
std::cout << TEXT("No text found for this error number...") << std::endl;
//::MessageBox(NULL, TEXT("No text found for this error number..."), TEXT("Tip"), MB_OK);
}
}
int main()
{
HANDLE hFile = CreateFile(TEXT("c:\\ben"), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
ShowLastErrorMessage(GetLastError());
}
else
{
std::cout << "Create file Successed.... " << std::endl;
return 0;
}
}
GetLastError 从数字ID转化成 文本
最新推荐文章于 2021-08-26 16:37:53 发布