Drecik学习经验分享
转载请注明出处:http://blog.csdn.net/drecik__/article/details/8078785
使用函数FormatMessage函数来查找错误信息,具体操作查看代码
BOOL ShowErrorInfo( DWORD dwError )
{
HLOCAL hLocal = NULL;
// 使用系统默认语言返回错误信息;
DWORD dwSystemLocale = MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL );
// 在系统默认位置寻找错误信息;
BOOL bOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | // 希望返回与系统定义的错误代码对应的字符串;
FORMAT_MESSAGE_IGNORE_INSERTS | // 允许含有%占位符的信息;
FORMAT_MESSAGE_ALLOCATE_BUFFER, // 分配足够到的内存;
NULL, dwError, dwSystemLocale, (PTSTR)&hLocal, 0, NULL );
// 未找到在网络错误信息中查找;
if ( !bOk )
{
HMODULE hNetDll = LoadLibraryEx( TEXT("netmsg.dll"), NULL,
DONT_RESOLVE_DLL_REFERENCES );
if ( hNetDll )
{
bOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
hNetDll, dwError, dwSystemLocale, (PTSTR)&hLocal, 0, NULL );
}
FreeLibrary(hNetDll);
}
if ( bOk && hLocal )
{
// 显示错误信息;
MessageBox( NULL, (PCTSTR)LocalLock(hLocal), TEXT("Error"), 0 );
LocalFree( hLocal );
return TRUE;
}
return FALSE;
}