FormatMessage of Windows API

Source: Internet
Author: User

    • FormatMessage

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.