2 springontime springontime 于 2014.11.23 10:31 提问

GetLastError的使用安全么

我不是很清楚错误码是线程私有的还是整个操作系统共享的,所以提出此问题,经常看到很多人用GetLastError来获取前一个语句可能出现错误时的错误码来决定后续的操作步骤,但我想知道,如果在发生错误和调用GetLastError中间被其它线程或者进程给改变了错误码,那岂不是得不到真正的错误原因?

4个回答

caozhy
caozhy   Ds   Rxr 2014.11.23 21:11

相对进程是独立的,相对线程是共享的。

lovesmiles
lovesmiles   Rxr 2014.11.25 09:38

是的,你说对了。GetLastError是线程不安全的。

zhao4zhong1
zhao4zhong1   Rxr 2014.11.25 15:43

安全。因为:GetLastError
The GetLastError function returns 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.

DWORD GetLastError(VOID)

Parameters
This function has no parameters.

Return Values
The return value is the calling thread's last-error code value. Functions set this value by calling the SetLastError function. The Return Value section of each reference page notes the conditions under which the function sets the last-error code.

Windows 95 and Windows 98: Because SetLastError is a 32-bit function only, Win32 functions that are actually implemented in 16-bit code do not set the last-error code. You should ignore the last-error code when you call these functions. They include window management functions, GDI functions, and Multimedia functions.

Remarks
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(0) when they succeed, wiping out the error code set by the most recently failed function.

Most functions in the Win32 API that set the thread's last error code value set it when they fail; a few functions set it when they succeed. Function failure is typically indicated by a return value error code such as FALSE, NULL, 0xFFFFFFFF, or –1. Some functions call SetLastError under conditions of success; those cases are noted in each function's reference page.

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.

To obtain an error string for system error codes, use the FormatMessage function. For a complete list of error codes, see Error Codes or the SDK header file WINERROR.H.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.

See Also
Errors Overview, Error Functions, FormatMessage, SetLastError, SetLastErrorEx

查MSDN是Windows程序员必须掌握的技能之一。

VisualEleven
VisualEleven   Ds   Rxr 2014.12.03 19:28

安全。你得知道还有一个线程局部存储TLS

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!