Windows API 之 FormatMessage

  • 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 identifier. The function copies the formatted message text to an 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,
  _In_opt_ va_list *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 terminating null character.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

举例:

#include <iostream>
#include <windows.h>
#include <strsafe.h>

int main()
{
    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,  //输入的错误编号
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&lpMsgBuff,  //得到错误说明
        0, NULL);
lpDisplayBuff
= (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuff) + lstrlen((LPCTSTR)lpszFunction) + 40)*sizeof(TCHAR));  //为StringCchPrintf初始化Buffer StringCchPrintf((LPTSTR)lpDisplayBuff,  //StringCcPrintf用来格式化字符串 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; }

 输出结果:

 

参考:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx

http://blog.csdn.net/zhao_yin/article/details/6989495

转载于:https://www.cnblogs.com/predator-wang/p/4985021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值