System Error Code
添加时间:2013-5-15 点击量:
When you call a Windows function, it validates the parameters that you pass to it and then attempts to perform its duty. If you pass an invalid parameter or if for some other reason the action cannot be performed, the functions return value indicates that the function failed in some way.
When a Windows function returns with an error code, its frequently useful to understand why the function failed. Microsoft has compiled a list of all possible error codes and has assigned each error code a 32-bit number.
Internally, when a Windows function detects an error, it uses a mechanism called thread-local storage to associate the appropriate error-code number with the calling thread.
This mechanism allows threads to run independently of each other without affecting each others error codes. When the function returns to you, its return value indicates that an error has occurred. To see exactly which error this is, call the GetLastError function:
DWORD GetLastError();
This function simply returns the threads 32-bit error code set by the last function call. Each error has three representations: a message ID (a macro that you can use in your source code to compare against the return value of GetLastError), message text (an English text description of the error), and a number (which you should avoid using and instead use the message ID).
When a Windows function fails, you should call GetLastError right away because the value is very likely to be overwritten if you call another Windows function. Notice that a Windows function that succeeds might overwrite this value with ERROR_SUCCESS.
While debugging, I find it extremely useful to monitor the threads last error code. In Microsoft Visual Studio, Microsofts debugger supports a useful feature—you can configure the Watch window to always show you the threads last error code number and the text description of the error. This is done by ing a row in the Watch window and typing ¥err,hr.
Visual Studio also ships with a small utility called Error Lookup. You can use Error Lookup to convert an error code number into its textual description.
When you call a Windows function, it validates the parameters that you pass to it and then attempts to perform its duty. If you pass an invalid parameter or if for some other reason the action cannot be performed, the functions return value indicates that the function failed in some way.
When a Windows function returns with an error code, its frequently useful to understand why the function failed. Microsoft has compiled a list of all possible error codes and has assigned each error code a 32-bit number.
Internally, when a Windows function detects an error, it uses a mechanism called thread-local storage to associate the appropriate error-code number with the calling thread.
This mechanism allows threads to run independently of each other without affecting each others error codes. When the function returns to you, its return value indicates that an error has occurred. To see exactly which error this is, call the GetLastError function:
DWORD GetLastError();
This function simply returns the threads 32-bit error code set by the last function call. Each error has three representations: a message ID (a macro that you can use in your source code to compare against the return value of GetLastError), message text (an English text description of the error), and a number (which you should avoid using and instead use the message ID).
When a Windows function fails, you should call GetLastError right away because the value is very likely to be overwritten if you call another Windows function. Notice that a Windows function that succeeds might overwrite this value with ERROR_SUCCESS.
While debugging, I find it extremely useful to monitor the threads last error code. In Microsoft Visual Studio, Microsofts debugger supports a useful feature—you can configure the Watch window to always show you the threads last error code number and the text description of the error. This is done by ing a row in the Watch window and typing ¥err,hr.
Visual Studio also ships with a small utility called Error Lookup. You can use Error Lookup to convert an error code number into its textual description.