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应用程序的功能或者能够使两个或多个 ...
随机推荐
- (repost)在ARM Linux内核中增加一个新的系统调用
实验平台内核版本为4.0-rc1,增加一仅仅打印Hello World的syscall,最后我们在用户空间swi验证 实验平台内核版本为4.0-rc1,增加的系统调用仅仅是简单打印一个Hello Wo ...
- Quartz的cron表达式
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...
- YII2.0 数据库增删改查
/*==================== dkhBaseModel 数据库增删改查方法 start ================================*/ //新增一条数据 publ ...
- Struts2第二天
Struts2第二天 昨天: 1.Action的编写方式:实现Action接口.继承ActionSupport.自定义pojo作为action 2.action调用方法:默认的execute.meth ...
- css实现三列布局,左右固定值,中间自适应。
这里主要用到的是position:absolute;及margin属性;代码很简单,一看就明白. <!DOCTYPE html> <html lang="zh_CN&quo ...
- nodejs url方法
ulrl方法 url.format(urlObj) //将对象装换成url url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) / ...
- 老oj1965:polygon半平面交
题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965 polygon半平面交 Time Limit:1000MS ...
- 千万PV级别WEB站点架构设计
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog.51cto.com/353572/1369762 高性能与多 ...
- 多线程---同步函数的锁是this(转载)
class Ticket implements Runnable { private int tick = 100; Object obj = new Object(); boolean flag = ...
- sharepoint2010配置个人网站的offical方法 来自Jetluning的专栏
Configuring My Site in SharePoint 201 SharePoint My Sites are commonly referred to as “Facebook fo ...