网站首页  词典首页

请输入您要查询的函数:

 

术语 formatmessage
释义 FormatMessage
语法:
C++
DWORD WINAPI FormatMessage(
__in DWORD dwFlags,
__in_opt LPCVOID lpSource,
__in DWORD dwMessageId,
__in DWORD dwLanguageId,
__out LPTSTR lpBuffer,
__in DWORD nSize,
__in_opt va_list *Arguments
);
FormatMessage函数
格式化消息字符串。该函数需要一个作为输入信息的定义。该消息的定义可以来自传递给函数的缓冲区。它可以来自一个消息表资源在一个已经加载的模块。或来电者可以要求的功能来搜索邮件定义系统的消息表资源(拧)。函数正处在一个信息表上的信息识别和语言为基础的资源标识符的消息定义。函数复制文本格式的邮件到一个输出缓冲区,处理任何提出要求的嵌入式插入序列。
参数
dwFlags [in]
格式选项,以及如何解释lpSource参数。低字节顺序的dwFlags指定函数如何处理输出缓冲区中换行符。低字节顺序也可以指定一个格式化的输出行的最大宽度。
此参数可以是一个或多个下列值。
ValueMeaning
FORMAT_MESSAGE_ALLOCATE_BUFFER
0x00000100The函数分配一个足够大的缓冲区来保存格式的邮件,并将其放置在由lpBuffer指定的地址的指针分配的缓冲区。该lpBuffer参数是一个LPTSTR指针,您必须转换指针一LPTSTR(例如,(LPTSTR)&lpBuffer)。在nSize参数指定TCHARs最低数量分配为输出消息缓冲区。打电话者应使用LocalFree函数来释放缓冲区时,不再需要。
FORMAT_MESSAGE_ARGUMENT_ARRAY
参数0x00002000The参数不是一个va_list结构,而是一个数组的指针的值代表的论点。
这标志不能用于64位整数值。如果您使用的是64位整数,您必须使用va_list结构。
FORMAT_MESSAGE_FROM_HMODULE
0x00000800The lpSource参数是一个包含模块句柄消息表资源(S)以搜索。如果这lpSource句柄是NULL,当前进程的程序映像文件将被搜查。这标志不能用于FORMAT_MESSAGE_FROM_STRING。
如果模块没有消息表资源,该函数失败,ERROR_RESOURCE_TYPE_NOT_FOUND。
FORMAT_MESSAGE_FROM_STRING
0x00000400The lpSource参数是一个空指针结尾的字符串,其中包含一个消息定义。该消息可能包含定义插入序列,作为一条消息表资源的消息文本只是可能。这标志不能用于FORMAT_MESSAGE_FROM_HMODULE或FORMAT_MESSAGE_FROM_SYSTEM。
FORMAT_MESSAGE_FROM_SYSTEM
0x00001000The职能应搜索系统消息表资源(s)对要求的讯息。如果此标志与FORMAT_MESSAGE_FROM_HMODULE指定,功能搜索系统信息表,如果消息不是由lpSource指定的模块中找到。这标志不能用于FORMAT_MESSAGE_FROM_STRING。
如果这个标志被指定,应用程序可以通过GetLastError函数的结果检索系统消息文本定义的错误。
FORMAT_MESSAGE_IGNORE_INSERTS
在邮件的定义0x00000200Insert序列被忽略,并通过向输出缓冲区不变通过。此标志可用于提取后格式化消息。如果此标志设置的参数参数被忽略。
低字节顺序的dwFlags可以指定一个格式化的输出行的最大宽度。以下是在低位字节的可能值。
ValueMeaning
0There没有输出线宽度的限制。函数存储换行符,在消息定义文本的到输出缓冲区。
FORMAT_MESSAGE_MAX_WIDTH_MASK
0x000000FFThe函数忽略文本的消息定义定期换行符。函数存储硬编码到输出缓冲区的消息定义文本换行符。函数产生任何新的换行符。
如果低位字节是一个非零值比FORMAT_MESSAGE_MAX_WIDTH_MASK其他,它指定一个输出行的最大字符数。该函数忽略文本的消息定义定期换行符。该函数没有分割由跨线中断空格分隔的字符串。函数存储硬编码到输出缓冲区的消息定义文本换行符。硬编码换行符是与%不适用转义序列编码。
lpSource [中,可选]
该邮件的定义的位置。该参数的类型取决于在dwFlags参数的设置。
dwFlags SettingMeaning
FORMAT_MESSAGE_FROM_HMODULE
0x00000800A处理的模块,它包含的信息表进行搜索。
FORMAT_MESSAGE_FROM_STRING
0x00000400Pointer的字符串,格式化的消息的文本组成。这将是扫描插入和相应格式化。
如果没有这些标志是在dwFlags设置,那么lpSource被忽略。
dwMessageId [in]
所请求的信息消息标识符。此参数被忽略,如果dwFlags包含FORMAT_MESSAGE_FROM_STRING。
dwLanguageId [in]
所请求的信息语言标识符。此参数被忽略,如果dwFlags包含FORMAT_MESSAGE_FROM_STRING。
如果传递一个参数,在此特定的LangID,FormatMessage会返回一个消息,只有语言ID。如果该函数无法找到该语言标识,它返回ERROR_RESOURCE_LANG_NOT_FOUND消息。如果您在零传球,FormatMessage查找了下列顺序LANGIDs消息:
语言中立
线程的LangID基于线程的区域设置值
用户的默认语言标识,根据用户的默认语言环境的价值
系统默认的语言标识的基础上,系统默认区域设置值
美国英语
如果FormatMessage没有找到对上述LANGIDs任何一条消息,它返回的任何语言消息字符串存在。如果失败,则返回ERROR_RESOURCE_LANG_NOT_FOUND。
lpBuffer [out]
阿一个缓冲区,它接收NULL结尾的字符串,指定格式的邮件指针。如果dwFlags包括FORMAT_MESSAGE_ALLOCATE_BUFFER,函数分配一个缓冲区使用LocalAlloc函数,和地方在lpBuffer指定的地址指针到缓冲区。
此缓冲区不能大于64K字节。
nSize [in]
如果FORMAT_MESSAGE_ALLOCATE_BUFFER标志没有被设置,此参数指定输出缓冲区的大小TCHARs。如果FORMAT_MESSAGE_ALLOCATE_BUFFER设置,此参数指定TCHARs最低数量分配一个输出缓冲区。
输出缓冲区不能大于64K字节。
参数[中,可选]
一个是如格式的邮件插入值使用值的数组。一个格式字符串%1显示,在参数数组的第一个值,1%,2表示第二个参数,等等。
每个值的解释取决于与该消息定义插入相关的格式信息。默认是把每个值作为一个NULL结尾的字符串指针。
默认情况下,参数参数的类型为va_list *,这是语言和执行具体的数据描述一个可变数目的参数的类型。该va_list参数是不确定的状态后,从函数返回。要使用va_list再次,破坏可变参数列表的指针,并使用va_end与va_start初始化它。
如果您没有一个类型的指针va_list *,然后指定FORMAT_MESSAGE_ARGUMENT_ARRAY国旗和一个指针传递给一个DWORD_PTR值的数组,这些值输入值的插入格式化的消息。每个插入必须有一个数组中的相应元素。
返回值
如果函数成功,返回值是在输出缓冲储存TCHARs数,不包括终止空字符。
如果函数失败,返回值是零。为了获得更多错误信息,调用GetLastError。
备注
Within the message text, several escape sequences are supported for dynamically formatting the message. These escape sequences and their meanings are shown in the following tables. All escape sequences start with the percent character (%).
Escape sequenceMeaning
%0Terminates a message text line without a trailing new line character. This escape sequence can be used to build up long lines or to terminate the message itself without a trailing new line character. It is useful for prompt messages.
%n!format string!Identifies an insert. The value of n can be in the range from 1 through 99. The format string (which must be surrounded by exclamation marks) is optional and defaults to !s! if not specified. For more information, see Format Specification Fields .
The format string can include a width and precision specifier for strings and a width specifier for integers. Use an asterisk (*) to specify the width and precision. For example, %1!*.*s! or %1!*u!.
If you do not use the width and precision specifiers, the insert numbers correspond directly to the input arguments. For example, if the source string is "%1 %2 %1" and the input arguments are "Bill" and "Bob", the formatted output string is "Bill Bob Bill".
However, if you use a width and precision specifier, the insert numbers do not correspond directly to the input arguments. For example, the insert numbers for the previous example could change to "%1!*.*s! %4 %5!*s!".
The insert numbers depend on whether you use an arguments array (FORMAT_MESSAGE_ARGUMENT_ARRAY) or a va_list. For an arguments array, the next insert number is n+2 if the previous format string contained one asterisk and is n+3 if two asterisks were specified. For a va_list, the next insert number is n+1 if the previous format string contained one asterisk and is n+2 if two asterisks were specified.
If you want to repeat "Bill", as in the previous example, the arguments must include "Bill" twice. For example, if the source string is "%1!*.*s! %4 %5!*s!", the arguments could be, 4, 2, Bill, Bob, 6, Bill (if using the FORMAT_MESSAGE_ARGUMENT_ARRAY flag). The formatted string would then be " Bi Bob Bill".
Repeating insert numbers when the source string contains width and precision specifiers may not yield the intended results. If you replaced %5 with %1, the function would try to print a string at address 6 (likely resulting in an access violation).
Floating-point format specifiers-e, E, f, and g-are not supported. The workaround is to use the StringCchPrintf function to format the floating-point number into a temporary buffer, then use that buffer as the insert string.
Inserts that use the I64 prefix are treated as two 32-bit arguments. They must be used before subsequent arguments are used. Note that it may be easier for you to use StringCchPrintf instead of this prefix.

Any other nondigit character following a percent character is formatted in the output message without the percent character. Following are some examples.
Format stringResulting output
%%A single percent sign.
%spaceA single space. This format string can be used to ensure the appropriate number of trailing spaces in a message text line.
%.A single period. This format string can be used to include a single period at the beginning of a line without terminating the message text definition.
%!A single exclamation point. This format string can be used to include an exclamation point immediately after an insert without its being mistaken for the beginning of a format string.
%nA hard line break when the format string occurs at the end of a line. This format string is useful when FormatMessage is supplying regular line breaks so the message fits in a certain width.
%rA hard carriage return without a trailing newline character.
%tA single tab.

Security Remarks
If this function is called without FORMAT_MESSAGE_IGNORE_INSERTS, the Arguments parameter must contain enough parameters to satisfy all insertion sequences in the message string, and they must be of the correct type. Therefore, do not use untrusted or unknown message strings with inserts enabled because they can contain more insertion sequences than Arguments provides, or those they may be of the wrong type. In particular, it is unsafe to take an arbitrary system error code returned from an API and use FORMAT_MESSAGE_FROM_SYSTEM without FORMAT_MESSAGE_IGNORE_INSERTS.
实例
该FormatMessage函数可用于获取对系统的GetLastError函数返回错误代码的错误消息字符串。有关示例,请参见Last-Error。
下面的示例演示如何使用参数数组的宽度和精度说明。
#ifndef UNICODE
#define UNICODE
#endif
#include
#include
void main(void)
{
LPWSTR pMessage = L"%1!*.*s! %4 %5!*s!";
DWORD_PTR pArgs[] = { (DWORD_PTR)4, (DWORD_PTR)2, (DWORD_PTR)L"Bill", // %1!*.*s! refers back to the first insertion string in pMessage
(DWORD_PTR)L"Bob", // %4 refers back to the second insertion string in pMessage
(DWORD_PTR)6, (DWORD_PTR)L"Bill" }; // %5!*s! refers back to the third insertion string in pMessage
const DWORD size = 100+1;
WCHAR buffer[size];

if (!FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
pMessage,
0,
0,
buffer,
size,
(va_list*)pArgs))
{
wprintf(L"Format message failed with 0x%x\\n", GetLastError());
return;
}
// Buffer contains " Bi Bob Bill".
wprintf(L"Formatted message: %s\\n", buffer);
}

The following example shows how to implement the previous example using va_list.
#ifndef UNICODE
#define UNICODE
#endif
#include
#include
LPWSTR GetFormattedMessage(LPWSTR pMessage, ...);
void main(void)
{
LPWSTR pBuffer = NULL;
LPWSTR pMessage = L"%1!*.*s! %3 %4!*s!";
// The variable length arguments correspond directly to the format
// strings in pMessage.
pBuffer = GetFormattedMessage(pMessage, 4, 2, L"Bill", L"Bob", 6, L"Bill");
if (pBuffer)
{
// Buffer contains " Bi Bob Bill".
wprintf(L"Formatted message: %s\\n", pBuffer);
LocalFree(pBuffer);
}
else
{
wprintf(L"Format message failed with 0x%x\\n", GetLastError());
}
}
// Formats a message string using the specified message and variable
// list of arguments.
LPWSTR GetFormattedMessage(LPWSTR pMessage, ...)
{
LPWSTR pBuffer = NULL;
va_list args = NULL;
va_start(args, pMessage);
FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
pMessage,
0,
0,
(LPWSTR)&pBuffer,
0,
&args);
va_end(args);
return pBuffer;
}
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinbase.h(头文件:winuser.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode和ANSI namesFormatMessageW(Unicode)和FormatMessageA(ANSI)的
参见
错误处理函数
信息表
消息编译器
信息表
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年7月30日
==英文原文==FormatMessage Function
Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
Syntax
C++
DWORD WINAPI FormatMessage(
__in DWORD dwFlags,
__in_opt LPCVOID lpSource,
__in DWORD dwMessageId,
__in DWORD dwLanguageId,
__out LPTSTR lpBuffer,
__in DWORD nSize,
__in_opt va_list *Arguments
);
Parameters
dwFlags [in]
The formatting options, 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 one or more of the following values.
ValueMeaning
FORMAT_MESSAGE_ALLOCATE_BUFFER
0x00000100The 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 lpBuffer parameter is a pointer to an LPTSTR; you must cast the pointer to an LPTSTR (for example, (LPTSTR)&lpBuffer). The nSize parameter specifies the minimum number of TCHARs to allocate for an output message buffer. The caller should use the LocalFree function to free the buffer when it is no longer needed.
FORMAT_MESSAGE_ARGUMENT_ARRAY
0x00002000The Arguments parameter is not a va_list structure, but is a pointer to an array of values that represent the arguments.
This flag cannot be used with 64-bit integer values. If you are using a 64-bit integer, you must use the va_list structure.
FORMAT_MESSAGE_FROM_HMODULE
0x00000800The lpSource parameter is a module handle containing the message-table resource(s) to search. If this lpSource handle is NULL, the current process's application image file will be searched. This flag cannot be used with FORMAT_MESSAGE_FROM_STRING.
If the module has no message table resource, the function fails with ERROR_RESOURCE_TYPE_NOT_FOUND.
FORMAT_MESSAGE_FROM_STRING
0x00000400The lpSource parameter is a pointer to a null-terminated string that contains a message definition. The message definition may contain insert sequences, just as the message text in a message table resource may. This flag cannot be used with FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM.
FORMAT_MESSAGE_FROM_SYSTEM
0x00001000The function should search the system message-table resource(s) 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. This flag 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.
FORMAT_MESSAGE_IGNORE_INSERTS
0x00000200Insert 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, the Arguments parameter is ignored.

The low-order byte of dwFlags can specify the maximum width of a formatted output line. The following are possible values of the low-order byte.
ValueMeaning
0There are no output line width restrictions. The function stores line breaks that are in the message definition text into the output buffer.
FORMAT_MESSAGE_MAX_WIDTH_MASK
0x000000FFThe 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.

If the low-order byte is a nonzero value other than FORMAT_MESSAGE_MAX_WIDTH_MASK, it specifies 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.
lpSource [in, optional]
The location of the message definition. The type of this parameter depends upon the settings in the dwFlags parameter.
dwFlags SettingMeaning
FORMAT_MESSAGE_FROM_HMODULE
0x00000800A handle to the module that contains the message table to search.
FORMAT_MESSAGE_FROM_STRING
0x00000400Pointer to a string that consists of unformatted message text. It will be scanned for inserts and formatted accordingly.

If neither of these flags is set in dwFlags, then lpSource is ignored.
dwMessageId [in]
The message identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.
dwLanguageId [in]
The language identifier for the requested message. This parameter is ignored if dwFlags includes FORMAT_MESSAGE_FROM_STRING.
If you pass a specific LANGID in this parameter, FormatMessage will return a message for that LANGID only. If the function cannot find a message for that LANGID, it returns ERROR_RESOURCE_LANG_NOT_FOUND. If you pass in zero, FormatMessage looks for a message for LANGIDs in the following order:
Language neutral
Thread LANGID, based on the thread's locale value
User default LANGID, based on the user's default locale value
System default LANGID, based on the system default locale value
US English
If FormatMessage does not locate a message for any of the preceding LANGIDs, it returns any language message string that is present. If that fails, it returns ERROR_RESOURCE_LANG_NOT_FOUND.
lpBuffer [out]
A pointer to a buffer that receives the null-terminated string that specifies the formatted message. If dwFlags includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the LocalAlloc function, and places the pointer to the buffer at the address specified in lpBuffer.
This buffer cannot be larger than 64K bytes.
nSize [in]
If the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the size of the output buffer, in TCHARs. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of TCHARs to allocate for an output buffer.
The output buffer cannot be larger than 64K bytes.
Arguments [in, optional]
An array of values that are used as insert values in the formatted message. A %1 in the format string indicates the first value in the Arguments array; a %2 indicates the second argument; and so on.
The interpretation of each 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 type va_list*, which is a language- and implementation-specific data type for describing a variable number of arguments. The state of the va_list argument is undefined upon return from the function. To use the va_list again, destroy the variable argument list pointer using va_end and reinitialize it with va_start.
If you do not have a pointer of type va_list*, then specify the FORMAT_MESSAGE_ARGUMENT_ARRAY flag and pass a pointer to an array of DWORD_PTR values; those values are input to the message formatted as the insert values. Each insert must have a corresponding element in the array.
Return Value
If the function succeeds, the return value is the number of TCHARs stored in the output buffer, excluding the terminating null character.
If the function fails, the return value is zero. To get extended error information, call GetLastError .
Remarks
Within the message text, several escape sequences are supported for dynamically formatting the message. These escape sequences and their meanings are shown in the following tables. All escape sequences start with the percent character (%).
Escape sequenceMeaning
%0Terminates a message text line without a trailing new line character. This escape sequence can be used to build up long lines or to terminate the message itself without a trailing new line character. It is useful for prompt messages.
%n!format string!Identifies an insert. The value of n can be in the range from 1 through 99. The format string (which must be surrounded by exclamation marks) is optional and defaults to !s! if not specified. For more information, see Format Specification Fields .
The format string can include a width and precision specifier for strings and a width specifier for integers. Use an asterisk (*) to specify the width and precision. For example, %1!*.*s! or %1!*u!.
If you do not use the width and precision specifiers, the insert numbers correspond directly to the input arguments. For example, if the source string is "%1 %2 %1" and the input arguments are "Bill" and "Bob", the formatted output string is "Bill Bob Bill".
However, if you use a width and precision specifier, the insert numbers do not correspond directly to the input arguments. For example, the insert numbers for the previous example could change to "%1!*.*s! %4 %5!*s!".
The insert numbers depend on whether you use an arguments array (FORMAT_MESSAGE_ARGUMENT_ARRAY) or a va_list. For an arguments array, the next insert number is n+2 if the previous format string contained one asterisk and is n+3 if two asterisks were specified. For a va_list, the next insert number is n+1 if the previous format string contained one asterisk and is n+2 if two asterisks were specified.
If you want to repeat "Bill", as in the previous example, the arguments must include "Bill" twice. For example, if the source string is "%1!*.*s! %4 %5!*s!", the arguments could be, 4, 2, Bill, Bob, 6, Bill (if using the FORMAT_MESSAGE_ARGUMENT_ARRAY flag). The formatted string would then be " Bi Bob Bill".
Repeating insert numbers when the source string contains width and precision specifiers may not yield the intended results. If you replaced %5 with %1, the function would try to print a string at address 6 (likely resulting in an access violation).
Floating-point format specifiers—e, E, f, and g—are not supported. The workaround is to use the StringCchPrintf function to format the floating-point number into a temporary buffer, then use that buffer as the insert string.
Inserts that use the I64 prefix are treated as two 32-bit arguments. They must be used before subsequent arguments are used. Note that it may be easier for you to use StringCchPrintf instead of this prefix.

Any other nondigit character following a percent character is formatted in the output message without the percent character. Following are some examples.
Format stringResulting output
%%A single percent sign.
%spaceA single space. This format string can be used to ensure the appropriate number of trailing spaces in a message text line.
%.A single period. This format string can be used to include a single period at the beginning of a line without terminating the message text definition.
%!A single exclamation point. This format string can be used to include an exclamation point immediately after an insert without its being mistaken for the beginning of a format string.
%nA hard line break when the format string occurs at the end of a line. This format string is useful when FormatMessage is supplying regular line breaks so the message fits in a certain width.
%rA hard carriage return without a trailing newline character.
%tA single tab.

Security Remarks
If this function is called without FORMAT_MESSAGE_IGNORE_INSERTS, the Arguments parameter must contain enough parameters to satisfy all insertion sequences in the message string, and they must be of the correct type. Therefore, do not use untrusted or unknown message strings with inserts enabled because they can contain more insertion sequences than Arguments provides, or those they may be of the wrong type. In particular, it is unsafe to take an arbitrary system error code returned from an API and use FORMAT_MESSAGE_FROM_SYSTEM without FORMAT_MESSAGE_IGNORE_INSERTS.
Examples
The FormatMessage function can be used to obtain error message strings for the system error codes returned by GetLastError. For an example, see Retrieving the Last-Error Code .
The following example shows how to use an argument array and the width and precision specifiers.
#ifndef UNICODE
#define UNICODE
#endif
#include
#include
void main(void)
{
LPWSTR pMessage = L"%1!*.*s! %4 %5!*s!";
DWORD_PTR pArgs[] = { (DWORD_PTR)4, (DWORD_PTR)2, (DWORD_PTR)L"Bill", // %1!*.*s! refers back to the first insertion string in pMessage
(DWORD_PTR)L"Bob", // %4 refers back to the second insertion string in pMessage
(DWORD_PTR)6, (DWORD_PTR)L"Bill" }; // %5!*s! refers back to the third insertion string in pMessage
const DWORD size = 100+1;
WCHAR buffer[size];

if (!FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
pMessage,
0,
0,
buffer,
size,
(va_list*)pArgs))
{
wprintf(L"Format message failed with 0x%x\\n", GetLastError());
return;
}
// Buffer contains " Bi Bob Bill".
wprintf(L"Formatted message: %s\\n", buffer);
}

The following example shows how to implement the previous example using va_list.
#ifndef UNICODE
#define UNICODE
#endif
#include
#include
LPWSTR GetFormattedMessage(LPWSTR pMessage, ...);
void main(void)
{
LPWSTR pBuffer = NULL;
LPWSTR pMessage = L"%1!*.*s! %3 %4!*s!";
// The variable length arguments correspond directly to the format
// strings in pMessage.
pBuffer = GetFormattedMessage(pMessage, 4, 2, L"Bill", L"Bob", 6, L"Bill");
if (pBuffer)
{
// Buffer contains " Bi Bob Bill".
wprintf(L"Formatted message: %s\\n", pBuffer);
LocalFree(pBuffer);
}
else
{
wprintf(L"Format message failed with 0x%x\\n", GetLastError());
}
}
// Formats a message string using the specified message and variable
// list of arguments.
LPWSTR GetFormattedMessage(LPWSTR pMessage, ...)
{
LPWSTR pBuffer = NULL;
va_list args = NULL;
va_start(args, pMessage);
FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
pMessage,
0,
0,
(LPWSTR)&pBuffer,
0,
&args);
va_end(args);
return pBuffer;
}
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesFormatMessageW (Unicode) and FormatMessageA (ANSI)
See Also
Error Handling Functions
Message Tables
Message Compiler
Message Tables
Send comments about this topic to Microsoft
Build date: 7/30/2009
==原始网址==http://msdn.microsoft.com/en-us/library/ms679351(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 11:32:05