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, //得到错误说明
, 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的更多相关文章
- FormatMessage与GetLastError配合使用,排查windows api调用过程中的错误
前一段时间在学习windows api调用过程中,遇到过一些调用错误或者程序没能显示预期的结果,或者直接出现vc运行时错误. 这对新手来说是司空见惯的事,因为不太熟悉难免会出错,出错的信息如果能显示很 ...
- Windows API 错误码
在多数情况下,windows API在发生错误时很少抛出异常,多数是通过函数返回值进行处理.(windows api中无返回值的函数很少.) windows api错误处理通常按照以下方式:首先api ...
- C# Windows API
API:应用程序接口(API:Application Program Interface)应用程序接口(API:application programming interface)是一组定义.程序及协 ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...
- Windows API Hooking in Python
catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...
- 初识【Windows API】--文本去重
最近学习操作系统中,老师布置了一个作业,运用系统调用函数删除文件夹下两个重复文本类文件,Linux玩不动,于是就只能在Windows下进行了. 看了一下介绍Windows API的博客: 点击打开 基 ...
- C#调用windows API的一些方法
使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. ...
- Qt中使用Windows API
在Windows平台上进行开发,不可避免与Windows API打交道,Qt中使用的时候要添加对应API的头文件和链接lib文件,另外使用的Windows API的代码部分要使用#ifdef Q_O ...
- 在VBA中使用Windows API
VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...
随机推荐
- docker命令和后台参数
Docker官方为了让用户快速了解Docker,提供了一个 交互式教程 ,旨在帮助用户掌握Docker命令行的使用方法. Docker 命令行 下面对Docker的命令清单进行简单的介绍,详细内容在后 ...
- 1.1 java学习网站
1.http://www.rupeng.com/Courses/Index/51 2.https://www.zhihu.com/question/25255189
- JAVA类与对象(课堂总结)
一:"=="的不同含义 当"=="施加于原始数据类型变量时,是比较变量所保存的数据是否相等当"=="施加于引用类型变量时,是比较这两个变量是 ...
- js 对象类型 (对象的属性 ,对象的方法) this 关键字
$(function () { var observation = { init: function () { this.render();//断点:this bind :function() che ...
- CustomSummaryCalculate 用法
private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs ...
- android 实现透明状态栏
主要使用https://github.com/jgilfelt/SystemBarTint这个开源库 1 ,导入SystemBarTintManager类 2 ,BaseFragmentActivit ...
- ios根据文本自适应 然后 搭建类似如下效果
UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tbSecond.size.width, 0)]; hea ...
- C#使用IHttpModule接口修改http输出的方法浅谈
一.但你每次请求浏览一个页面,比如Login.aspx的时候,都会执行配置文件中system.webserver内的model这个节点的东西(这个是属于遍历的逻辑执行,会将model这个节点的东西全部 ...
- 《JS正则表达式》
1.精通 JS正则表达式: http://www.cnblogs.com/aaronjs/archive/2012/06/30/2570970.html 2.js常用正则表达式: http://www ...
- block的内部实现原理
一.简单定义 block是一个指向结构体的指针,编译器将block内部代码生成对应的函数,上述结构体中的函数指针(funcPtr)指向该函数的实现: 二.相关概念 形参和实参 形参:形式参数,用于定义 ...