MFC GetLastError 函数


Retrieves the calling thread’s last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other’s last-error code.
【检索调用线程的最后一个错误代码值。最后一个错误代码是按线程维护的。多个线程不会覆盖彼此的上一个错误代码。】

Visual Basic: Applications should call err.LastDllError instead of GetLastError.
【VB应该调用err.LastDllError而不是GetLastError。】

Syntax(语法)

DWORD WINAPI GetLastError(void);

Parameters(参数)

This function has no parameters.
【此函数没有参数。】

Return Value(返回值)

The return value is the calling thread’s last-error code.
【返回值是调用线程的最后一个错误代码。】

The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code. Most functions that set the thread’s last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.
【文档中用于设置最后一个错误代码的每个函数的返回值部分都会记录函数设置最后一个错误代码的条件。大多数设置线程最后一个错误代码的函数都会在失败时设置它。但是,有些函数在成功时也会设置最后一个错误代码。如果没有记录函数以设置最后一个错误代码,则此函数返回的值只是最近设置的最后一个错误代码;某些函数在成功时将最后一个错误代码设置为0,而其他函数则不设置。】

Remarks(备注)

Functions executed by the calling thread set this value by calling the SetLastError function. You should call the GetLastError function immediately when a function’s return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function.
【调用线程执行的函数通过调用SetLastError函数来设置此值。当函数的返回值指示这样的调用将返回有用的数据时,应该立即调用GetLastError函数。这是因为有些函数在成功调用SetLastError时使用了零,清除了最近失败的函数设置的错误代码。】

To obtain an error string for system error codes, use the FormatMessage function. For a complete list of error codes provided by the operating system, see System Error Codes.
【要获取系统错误代码的错误字符串,请使用FormatMessage函数。有关操作系统提供的错误代码的完整列表,请参阅系统错误代码。】

The error codes returned by a function are not part of the Windows API specification and can vary by operating system or device driver. For this reason, we cannot provide the complete list of error codes that can be returned by each function. There are also many functions whose documentation does not include even a partial list of error codes that can be returned.
【函数返回的错误代码不属于Windows API规范的一部分,可能因操作系统或设备驱动程序而异。因此,我们无法提供每个函数可以返回的错误代码的完整列表。还有许多函数的文档甚至不包括可以返回的错误代码的部分列表。】

Error codes are 32-bit values (bit 31 is the most significant bit). Bit 29 is reserved for application-defined error codes; no system error code has this bit set. If you are defining an error code for your application, set this bit to one. That indicates that the error code has been defined by an application, and ensures that your error code does not conflict with any error codes defined by the system.
【错误代码是32位值(位31是最高有效位)。位29是为应用程序定义的错误代码保留的;没有系统错误代码设置此位。如果要为应用程序定义错误代码,请将此位设置为1。表示错误代码已由应用程序定义,并确保您的错误代码与系统定义的任何错误代码不冲突。】

To convert a system error into an HRESULT value, use the HRESULT_FROM_WIN32 macro.
【要将系统错误转换为HRESULT值,请使用HRESULT_FROM_WIN32宏。】

心得体会

根据《Windows核心编程》第一章"错误处理"介绍,VS编译器在打断点时,可以在Watch窗口中,新增一行,Name输入"$err,hr",即可在断点调试过程中,实时监测当前线程的最后一个错误,如下图所示。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值