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 identif Ier. The function copies the formatted message text to a output buffer, processing any embedded insert sequences if requested.
DWORD WINAPI formatmessage ( _in_ DWORD dwFlags, _in_opt_ lpcvoid lpsource, _in_ DWORD Dwmessageid, _in_ DWORD dwlanguageid, _out_ LPTSTR lpbuffer, _in_ DWORD nSize, *Arguments);
Dwmessageid [in]
The message identifier for the requested message. This parameter is ignored if dwFlags includes format_message_from_string.
lpbuffer[out]
A pointer to a buffer, that receives the null-terminated string, that specifies the formatted message. If dwFlags includesformat_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.
Return value
If The function succeeds, the return value is the number of tchars stored in the output buffer, excluding the TER minating null character.
If The function fails, the return value is zero. To get extended error information, call GetLastError.
Example:
#include <iostream>#include<windows.h>#include<strsafe.h>intMain () {LPCTSTR lpszfunction="GetProcessID"; LPVOID Lpmsgbuff=NULL; LPVOID Lpdisplaybuff=NULL; DWORD dwerror=GetLastError (); FormatMessage (Format_message_allocate_buffer|Format_message_from_system|format_message_ignore_inserts, NULL, dwerror, //Input error number Makelangid (lang_neutral , Sublang_default), (LPTSTR)&Lpmsgbuff, //Get error description 0, NULL);
Lpdisplaybuff=(LPVOID) LocalAlloc (Lmem_zeroinit, (Lstrlen (LPCTSTR) lpmsgbuff)+ Lstrlen ((LPCTSTR) lpszfunction) + +)*sizeof(TCHAR)); //Initialize buffer for stringcchprintf stringcchprintf ((LPTSTR) Lpdisplaybuff, //stringccprintf used to format the string localsize (Lpdisplaybuff), TEXT ("%s failed with error%d:%s"), Lpszfunction, Dwerror, Lpmsgbuff); MessageBox (NULL, (LPCTSTR) Lpdisplaybuff, TEXT ("Error"), MB_OK); LocalFree (Lpdisplaybuff); System ("Pause"); return 0;}
Output Result:
Reference:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351 (v=vs.85). aspx
http://blog.csdn.net/zhao_yin/article/details/6989495
FormatMessage of Windows API