Windows核心編程
第一章 錯誤處理(Error Handling)
1.錯誤的表示
Windows在Winerr.h中定義了所有Win API的錯誤值。
一個錯誤值由三部分組成:Message ID, Message Text,一個數(shù)值。
如下:
// MessageId: ERROR_SUCCESS
// MessageText: The operation completed successfully.
#define ERROR_SUCCESS 0L
2.GetLastError/SetLastError/FormatMessage
GetLastError/SetLastError用于獲取和設(shè)置函數(shù)的錯誤值。
FormatMessage用于將錯誤值翻譯成相應(yīng)的字符串。
如:0L翻譯成 ”The operation completed successfully.”
頭文件:#include <windows.h>
3.錯誤值的結(jié)構(gòu)
錯誤值大于0代表函數(shù)調(diào)用成功。
Bits: | 31-30 | 29 | 28 | 27-16 | 15-0 |
---|---|---|---|---|---|
Contents | Severity | Microsoft/customer | Reserved | Facility code | Exception code |
Meaning | 0=Success 1 = Informational 2 = Warning 3 = Error | 0 = Microsoft-defined code 1 = customer-defined code | Must be 0 | The first 256 values are reserved by Microsoft | Microsoft/customer-defined code |
4. $err,hr
在調(diào)用時可以在Windows Watch中能過$err,hr 查看最后一個線程的函數(shù)調(diào)用時的錯誤值