如何获取错误消息说明使用 FormatMessage API
2007-01-03 16:07
1071 查看
来源:http://support.microsoft.com/kb/256348/zh-cn
注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。
#include <windows.h>
#include <lmerr.h>
#include <tchar.h>
#define ERRMSGBUFFERSIZE 256
void fnDisplayError( DWORD dwErrorMsgId )
...{
DWORD ret; // Temp space to hold a return value.
HINSTANCE hInst; // Instance handle for DLL.
HLOCAL pBuffer; // Buffer to hold the textual error description.
if ( HRESULT_FACILITY(dwErrorMsgId) == FACILITY_MSMQ )
...{ // MSMQ errors only (see winerror.h for facility info).
// Load the MSMQ library containing the error message strings.
hInst = LoadLibrary( TEXT("MQUTIL.DLL") );
if(hInst != 0)
...{ // hInst not NULL if the library was successfully loaded.
// Get the text string for a message definition
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // Function will handle memory allocation.
FORMAT_MESSAGE_FROM_HMODULE | // Using a module's message table.
FORMAT_MESSAGE_IGNORE_INSERTS,
hInst, // Handle to the DLL.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer that will hold the text string.
ERRMSGBUFFERSIZE, // Allocate at least this many chars for pBuffer.
NULL // No insert values.
);
} // hInst not NULL if the library was successfully loaded.
} // MSMQ errors only.
else if ( dwErrorMsgId >= NERR_BASE && dwErrorMsgId <= MAX_NERR )
...{ // Could be a network error.
// Load the library containing network messages.
hInst = LoadLibrary( TEXT("NETMSG.DLL") );
if(hInst != 0)
...{ // Not NULL if successfully loaded.
// Get a text string for the message definition.
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate memory for the message.
FORMAT_MESSAGE_FROM_HMODULE | // Message definition is in a module.
FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts used.
hInst, // Handle to the module containing the definition.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer to hold the text string.
ERRMSGBUFFERSIZE, // Smallest size that will be allocated for pBuffer.
NULL // No inserts.
);
} // Not NULL if successfully loaded.
} // Could be a network error.
else
...{ // Unknown message source.
// Get the message string from the system.
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate space for pBuffer.
FORMAT_MESSAGE_FROM_SYSTEM | // System wide message.
FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts.
NULL, // Message is not in a module.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer to hold the text string.
ERRMSGBUFFERSIZE, // The function will allocate at least this much for pBuffer.
NULL // No inserts.
);
}
// Display the string.
if( ret )
...{
_tprintf( _TEXT(" ERRORMESSAGE: %s "), (LPTSTR)pBuffer );
}
else
...{
_tprintf( _TEXT(" ERRORNUMBER: %d "), dwErrorMsgId );
}
// Free the buffer.
LocalFree( pBuffer );
}
注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。
概要
FormatMessage() API 调用可用于获取与 Microsoft 消息队列错误代码说明字符串。 FormatMessage 调用的 Mqutil.dll 模块句柄和用 FORMAT_MESSAGE_FROM_HMODULE 标志来检索消息文本更多信息
以下示例代码显示简单函数将其打印描述 Microsoft 消息队列错误消息到标准输出:#include <windows.h>
#include <lmerr.h>
#include <tchar.h>
#define ERRMSGBUFFERSIZE 256
void fnDisplayError( DWORD dwErrorMsgId )
...{
DWORD ret; // Temp space to hold a return value.
HINSTANCE hInst; // Instance handle for DLL.
HLOCAL pBuffer; // Buffer to hold the textual error description.
if ( HRESULT_FACILITY(dwErrorMsgId) == FACILITY_MSMQ )
...{ // MSMQ errors only (see winerror.h for facility info).
// Load the MSMQ library containing the error message strings.
hInst = LoadLibrary( TEXT("MQUTIL.DLL") );
if(hInst != 0)
...{ // hInst not NULL if the library was successfully loaded.
// Get the text string for a message definition
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // Function will handle memory allocation.
FORMAT_MESSAGE_FROM_HMODULE | // Using a module's message table.
FORMAT_MESSAGE_IGNORE_INSERTS,
hInst, // Handle to the DLL.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer that will hold the text string.
ERRMSGBUFFERSIZE, // Allocate at least this many chars for pBuffer.
NULL // No insert values.
);
} // hInst not NULL if the library was successfully loaded.
} // MSMQ errors only.
else if ( dwErrorMsgId >= NERR_BASE && dwErrorMsgId <= MAX_NERR )
...{ // Could be a network error.
// Load the library containing network messages.
hInst = LoadLibrary( TEXT("NETMSG.DLL") );
if(hInst != 0)
...{ // Not NULL if successfully loaded.
// Get a text string for the message definition.
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate memory for the message.
FORMAT_MESSAGE_FROM_HMODULE | // Message definition is in a module.
FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts used.
hInst, // Handle to the module containing the definition.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer to hold the text string.
ERRMSGBUFFERSIZE, // Smallest size that will be allocated for pBuffer.
NULL // No inserts.
);
} // Not NULL if successfully loaded.
} // Could be a network error.
else
...{ // Unknown message source.
// Get the message string from the system.
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate space for pBuffer.
FORMAT_MESSAGE_FROM_SYSTEM | // System wide message.
FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts.
NULL, // Message is not in a module.
dwErrorMsgId, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&pBuffer, // Buffer to hold the text string.
ERRMSGBUFFERSIZE, // The function will allocate at least this much for pBuffer.
NULL // No inserts.
);
}
// Display the string.
if( ret )
...{
_tprintf( _TEXT(" ERRORMESSAGE: %s "), (LPTSTR)pBuffer );
}
else
...{
_tprintf( _TEXT(" ERRORNUMBER: %d "), dwErrorMsgId );
}
// Free the buffer.
LocalFree( pBuffer );
}
相关文章推荐
- 如何获取错误消息说明使用 FormatMessage API
- C# WCF WinCE 解决方案 错误提示之:已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性
- python使用selenium获取元素出现错误:"errorMessage":"Compoud class names not permitted"
- 如何使用 Yahoo! Finance stock API 获取股票数据
- 如何使用 Yahoo! Finance stock API 获取股票数据
- 如何使用API获取文件系统的文件目录
- Mootools 1.4 官方网站的API使用说明存在错误
- 使用Wisdom RESTClient如何在Linux和Mac上获取测试报告和API文档?
- 使用Wisdom RESTClient如何在Linux和Mac上获取测试报告和API文档?
- 在对话框中使用CTreeCtrl控件时,在OnNMClick消息中如何获取选中的Item以及内容
- Linux如何获取命令使用帮助说明
- salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)
- 如何使用ansible获取被控制机上的haproxy转发消息请求数
- 在java程序中如何能获取到另一个程序的windows句柄,又如何使用这个句柄向这个窗口发送消息
- RevitAPI: 如何获取可以被明细表过滤器ScheduleFilter使用的参数Parameter?
- 如何使用 Regsvr32 工具以及对 Regsvr32 错误消息进行疑难解答
- 错误提示之:已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性
- 使用Wisdom RESTClient如何在Linux和Mac上获取测试报告和API文档?
- 关于C#中使用消息获取Message.LParam所包含的数据
- MessageFormat使用说明