通常,在写WINDOWS程序的时候我们会用 GetLastError()来获得错误代号,进而想要知道具体出错原因(文本描述),我们可以用 FormatMessage 函数来得到。
下面示例一段代码。
1
2 /*
3 利用FormatMessage函数得到错误代码的文本显示。而错误代号则可由GetLastError()获得。(_WINDOWS)
4 */
5
6
DWORD nErrorNo = GetLastError ( ); // 得到错误代码
7
LPSTR lpBuffer;
8
FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER
|
9
FORMAT_MESSAGE_IGNORE_INSERTS
|
10
FORMAT_MESSAGE_FROM_SYSTEM,
11
NULL,
12
nErrorNo, // 此乃错误代码,通常在程序中可由 GetLastError()得之
13
LANG_NEUTRAL,
14
(LPTSTR)
&
lpBuffer,
15
0
,
16
NULL );
17
CString strErrorCause
=
lpBuffer
?
_T(lpBuffer) : _T(
"
Sorry, cannot find this error info.
"
);
18
UpdateData (FALSE);
19
20
//
Free the buffer.
21
LocalFree (lpBuffer);
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
2 /*
3 利用FormatMessage函数得到错误代码的文本显示。而错误代号则可由GetLastError()获得。(_WINDOWS)
4 */
5
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
我这里编译了一个简单的可执行程序供给有需要的朋友 [ 下载 ]。
MD5: 50b4fd62cf3082a9a3131e0ca3a00a86 ErrorLookup.rar
截图:
![](https://i-blog.csdnimg.cn/blog_migrate/a75058faa3bc6eaf3abd11af4e27c97c.gif)