子弹 の VISIONS

NEVER back down ~~

C++博客 首页 新随笔 联系 聚合 管理
  112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks

        通常,在写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);

我这里编译了一个简单的可执行程序供给有需要的朋友 [ 下载 ]。
MD5: 50b4fd62cf3082a9a3131e0ca3a00a86  ErrorLookup.rar
截图:
posted on 2006-05-24 16:18 子弹のVISIONS 阅读(15212) 评论(4)  编辑 收藏 引用

Feedback

# 《Win32多线程程序设计》上的一个应用 2006-05-31 12:50 子弹


/*
* MtVerify.h
*
* Error handling for applications in
* "Multitheading Applications in Win32"
*
* The function PrintError() is marked as __inline so that it can be
* included from one or more C or C++ files without multiple definition
* errors. For the examples in this book, this works fine.
* To use the PrintError() in an application, it should be taken out,
* placed in its own source file, and the "__inline" declaration removed
* so the function will be globally available.
*/

#pragma comment( lib, "USER32" )

#include
#define MTASSERT(a) _ASSERTE(a)


#define MTVERIFY(a) if (!(a)) PrintError(#a,__FILE__, __LINE__, GetLastError())

__inline void PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
{
LPSTR lpBuffer;
char errbuf[256];
#ifdef _WINDOWS
char modulename[MAX_PATH];
#else // _WINDOWS
DWORD numread;
#endif // _WINDOWS

FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errnum,
LANG_NEUTRAL,
(LPTSTR)&lpBuffer,
0,
NULL );

wsprintf(errbuf, "\nThe following call failed at line %d in %s:\n\n"
" %s\n\nReason: %s\n", lineno, filename, linedesc, lpBuffer);
#ifndef _WINDOWS
WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, strlen(errbuf), &numread, FALSE );
Sleep(3000);
#else
GetModuleFileName(NULL, modulename, MAX_PATH);
MessageBox(NULL, errbuf, modulename, MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
#endif
exit(EXIT_FAILURE);
}

说明:其中输出到stderr和MessageBox部分可以把错误信息Log到文件或者其他存储中,以备分析错误记录。

MTVERIFY宏的使用前提是:产生的错误可以由GetLastError()捕捉。
  回复  更多评论
  

# re: GetLastError与FormatMessage联用得到出错原因 2008-02-28 18:15 匿名
3Q  回复  更多评论
  

# re: GetLastError与FormatMessage联用得到出错原因 2009-05-02 14:34 calm
lpBuffer:是一个指针
但在使用时 用( LPTSTR )&buffer,不就是变成指针的地址,也就是指针的指针了 这是什么原因?   回复  更多评论
  

# re: GetLastError与FormatMessage联用得到出错原因 2009-07-04 11:26 路过
@calm
因为这个API是传值,所以需要把指针的地址传过去,这样这个指针才能被API内部修改,指向他申请的内存空间。
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理