• 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,  //得到错误说明
, NULL);
lpDisplayBuff = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuff) + lstrlen((LPCTSTR)lpszFunction) + )*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 ;
}

输出结果:

参考:

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

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

Windows API 之 FormatMessage的更多相关文章

  1. FormatMessage与GetLastError配合使用,排查windows api调用过程中的错误

    前一段时间在学习windows api调用过程中,遇到过一些调用错误或者程序没能显示预期的结果,或者直接出现vc运行时错误. 这对新手来说是司空见惯的事,因为不太熟悉难免会出错,出错的信息如果能显示很 ...

  2. Windows API 错误码

    在多数情况下,windows API在发生错误时很少抛出异常,多数是通过函数返回值进行处理.(windows api中无返回值的函数很少.) windows api错误处理通常按照以下方式:首先api ...

  3. C# Windows API

    API:应用程序接口(API:Application Program Interface)应用程序接口(API:application programming interface)是一组定义.程序及协 ...

  4. Windows API 函数列表 附帮助手册

    所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...

  5. Windows API Hooking in Python

    catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...

  6. 初识【Windows API】--文本去重

    最近学习操作系统中,老师布置了一个作业,运用系统调用函数删除文件夹下两个重复文本类文件,Linux玩不动,于是就只能在Windows下进行了. 看了一下介绍Windows API的博客: 点击打开 基 ...

  7. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  8. Qt中使用Windows API

    在Windows平台上进行开发,不可避免与Windows API打交道,Qt中使用的时候要添加对应API的头文件和链接lib文件,另外使用的Windows API的代码部分要使用#ifdef  Q_O ...

  9. 在VBA中使用Windows API

    VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...

随机推荐

  1. 扔鸡蛋问题详解(Egg Dropping Puzzle)

    http://blog.csdn.net/joylnwang/article/details/6769160 经典的动态规划问题,题设是这样的:如果你有2颗鸡蛋,和一栋36层高的楼,现在你想知道在哪一 ...

  2. java 学习心得

  3. Linq入门

    一.Linq需要的C#语法支持: 1.隐式变量的使用var var使用时必须初始化      var是强类型数据 2.自动属性:public string FirstName{get ;set;} 3 ...

  4. Gym 101102B The Little Match Girl(贪心+规律)

    这个题目的做法不止一种,solve1:每个数字使用的火柴棒都在2~7之间,而且是连续的,就是2-7之前没有空着的数.这样就有一个结论,在下界为l,上界为r的情况下,假设有n个数,那么火柴棒总数一定在n ...

  5. Hibernate3 第三天

    Hibernate3 第三天 第一天:三个准备.七个步骤 第二天:一级缓存.快照.多对多和一对多的配置 学习内容: Hibernate的查询详解(各种检索(fetch)对象的方式) 1)条件查询分类( ...

  6. Javascript获取不重复的随机数值

    /** * 获取不重复随机数 * @param integer start 随机数最小值 * @param integer end 随机数最大值 * @param integer size 随机数获取 ...

  7. CodeForces 702A Maximum Increase

    简单$dp$. 如果$a[i]>a[i-1]$,那么$dp[i]=dp[i-1]+1$.否则,$dp[i]=1$.答案为$dp[i]$中的最大值. #pragma comment(linker, ...

  8. HDU 4990 Reading comprehension

    快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...

  9. VMWARE player 如何让 win2012 guest os 支持HYPER-V

    在 vm player 下安装了 win2012 r2, 但是启用 hyper-v的时候,提示不支持, 这时候要修改 Open the file Location for this Virtual M ...

  10. Ajax中参数带有html格式的 传入后台保存【一】

    因业务需求  要讲如下编辑器中带有样式的数据传入数据库保存 第一种方法  json格式传入 $(".privilegezn_page .btn_ok").click(functio ...