利用FormatMessage函数获取错误描述
来源:互联网 发布:授权回调域名校验出错 编辑:程序博客网 时间:2024/06/05 01:06
#include<iostream>#include<windows.h>#include <Locale.h>using namespace std;BOOL getErrorDescription(DWORD dwMessageId, HLOCAL &hlocal);int main(){DWORD dwMessageId;while (printf("Please Input MessageId:"), cin >> dwMessageId){HLOCAL hlocal = NULL;if (getErrorDescription(dwMessageId, hlocal)){printf("%s\n", (LPSTR)LocalLock(hlocal));LocalFree(hlocal);}else{cout << "ErrorDescription Not Found!" << endl;}}}BOOL getErrorDescription(DWORD dwMessageId, HLOCAL &hlocal){DWORD langid = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);BOOL fOK = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_IGNORE_INSERTS| FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMessageId, langid, (LPSTR)&hlocal, 0, NULL);if (!fOK){HMODULE hMod = LoadLibrary(TEXT("netmesg.dll"));if (hMod){fOK = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE, hMod, dwMessageId, langid, (LPSTR)&hlocal, 0, NULL);FreeLibrary(hMod);}}return fOK && hlocal != NULL;}
DWORD FormatMessage( DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPTSTR lpBuffer, DWORD nSize, va_list* Arguments);
- dwFlags
[in] Specifies aspects of the formatting process and how to interpret the lpSource parameter. The low-order byte of dwFlags specifies how the function handles line breaks in the output buffer. The low-order byte can also specify the maximum width of a formatted output line.
This parameter can be a combination of the following bit flags.
Value
Description
FORMAT_MESSAGE_ALLOCATE_BUFFER
Specifies that the lpBuffer parameter is a pointer to aPVOID pointer, and that the nSize parameter specifies the minimum number of bytes (ANSI version) or characters (Unicode version) to allocate for an output message buffer. The function allocates a buffer large enough to hold the formatted message, and places a pointer to the allocated buffer at the address specified by lpBuffer. The caller should use theLocalFree function to free the buffer when it is no longer needed.
FORMAT_MESSAGE_IGNORE_INSERTS
Specifies that insert sequences in the message definition are to be ignored and passed through to the output buffer unchanged. This flag is useful for fetching a message for later formatting. If this flag is set, theArguments parameter is ignored.
FORMAT_MESSAGE_FROM_STRING
Specifies that lpSource is a pointer to a null-terminated message definition. The message definition may contain insert sequences, just as the message text in a message table resource may. Cannot be used with FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM.
FORMAT_MESSAGE_FROM_HMODULE
Specifies that lpSource is a module handle containing the message-table resources to search. If thislpSource handle is NULL, the current process's application image file will be searched. Cannot be used with FORMAT_MESSAGE_FROM_STRING.
FORMAT_MESSAGE_FROM_SYSTEM
Specifies that the function should search the system message-table resources for the requested message. If this flag is specified with FORMAT_MESSAGE_FROM_HMODULE, the function searches the system message table if the message is not found in the module specified by lpSource. Cannot be used with FORMAT_MESSAGE_FROM_STRING.
If this flag is specified, an application can pass the result of the GetLastError function to retrieve the message text for a system-defined error.
Not all Windows Embedded Compact-based devices will contain the system message-table resources. This is a selectable part of the Windows Embedded Compact operating system and is often removed to conserve space.
FORMAT_MESSAGE_ARGUMENT_ARRAY
Specifies that the Arguments parameter isnot a va_list structure, but instead is just a pointer to an array of 32-bit values that represent the arguments.
The low-order byte of dwFlags can specify the maximum width of a formatted output line. Use the FORMAT_MESSAGE_MAX_WIDTH_MASK constant and bitwise Boolean operations to set and retrieve this maximum width value.
The following list shows how FormatMessage interprets the value of the low-order byte.
Value
Description
0
There are no output line width restrictions. The function stores line breaks that are in the message definition text into the output buffer.
A nonzero value other than FORMAT_MESSAGE_MAX_WIDTH_MASK
The nonzero value is the maximum number of characters in an output line. The function ignores regular line breaks in the message definition text. The function never splits a string delimited by white space across a line break. The function stores hard-coded line breaks in the message definition text into the output buffer. Hard-coded line breaks are coded with the %n escape sequence.
FORMAT_MESSAGE_MAX_WIDTH_MASK
The function ignores regular line breaks in the message definition text. The function stores hard-coded line breaks in the message definition text into the output buffer. The function generates no new line breaks.
- lpSource
[in] Pointer to the location of the message definition. The type of this parameter depends on the settings in thedwFlags parameter.
Value
Description
FORMAT_MESSAGE_FROM_HMODULE
The lpSource parameter is an hModule of the module that contains the message table to search.
FORMAT_MESSAGE_FROM_STRING
The lpSource parameter is an LPTSTR that points to unformatted message text. It will be scanned for inserts and formatted accordingly.
If neither of these flags is set in dwFlags, thenlpSource is ignored.
- dwMessageId
[in] Specifies the 32-bit message identifier for the requested message. This parameter is ignored ifdwFlags includes FORMAT_MESSAGE_FROM_STRING.
- dwLanguageId
[in] Not supported.
- lpBuffer
[out] Pointer to a buffer for the formatted (and null-terminated) message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using theLocalAlloc function, and places the pointer to the buffer at the address specified inlpBuffer.
- nSize
[in] If the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the maximum number of characters that can be stored in the output buffer. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of bytes or characters to allocate for an output buffer.
- Arguments
[in] Pointer to an array of 32-bit values that are used as insert values in the formatted message. A %1 in the format string indicates the first value in theArguments array; a %2 indicates the second argument; and so on.
The interpretation of each 32-bit value depends on the formatting information associated with the insert in the message definition. The default is to treat each value as a pointer to a null-terminated string.
By default, the Arguments parameter is of typeva_list*, which is a language- and implementation-specific data type for describing a variable number of arguments. If you do not have a pointer of typeva_list*, then specify the FORMAT_MESSAGE_ARGUMENT_ARRAY flag and pass a pointer to an array of 32-bit values; those values are input to the message formatted as the insert values. Each insert must have a corresponding element in the array.
The number of characters stored in the output buffer, excluding the terminating null character, indicates success. Zero indicates failure. To get extended error information, callGetLastError.
- 利用FormatMessage函数获取错误描述
- FormatMessage获取错误代码描述
- 使用FormatMessage获取由GetLastError得到的错误的描述
- FormatMessage错误消息处理函数
- FormatMessage函数
- FormatMessage函数
- FormatMessage函数
- FormatMessage 函数
- 如何获取错误消息说明使用 FormatMessage API
- 使用FormatMessage函数编写一个内核错误码查看器
- FormatMessage();错误代码to文本描述
- FormatMessage 错误消息
- FormatMessage VC函数
- FormatMessage函数的用法
- FormatMessage函数简介
- FormatMessage函数使用详解
- FormatMessage函数的用法
- FormatMessage函数的用法
- 利用NetFuke进行内网嗅探
- typedef函数指针用法
- UVA - 1588 Kickdown
- 类前置声明的使用
- 静态链接与动态链接的区别
- 利用FormatMessage函数获取错误描述
- UVA - 11809 Floating-Point Numbers
- socket和http间的区别
- 概念扫盲(对,这是发生在正文之前的知识点。)
- Erlang tool -- lager overload protection
- 差评using namespace std;
- 《从0到1》读书笔记第4章“竞争意识”第1记:雾失楼台,月迷津渡
- 二叉树各分支的和
- Android SDK开发包国内下载地址