书法字典:https://www.shufadict.com

Error Checking in DX

from http://nexe.gamedev.net/directKnowledge/default.asp?p=Debugging

When you're trying to find out why a specific function is failing - and the debug output isn't helping - you need to work with return codes. Note, however, that you should always check error codes, not just when debug output isn't useful. More or less all the functions in the DirectX API return HRESULT?s, the standard COM return type. Given an HRESULT? hr, you can check for success and failure using the following macros:
- SUCCEEDED(hr) will be true if hr indicates an 'ok', that is, the function you got the result from succeeded. We stress here that you should never compare an HRESULT? to S_OK or whatever to check for success, because success can be indicated by many values, not just S_OK.
- FAILED(hr) will be true if hr indicates a failure.
Additionally, the following functions and macros are helpful as well:
- DXGetErrorString9(hr) will return a string which is the name of the constant that hr represents. If hr were D3DERR_DEVICELOST, the string would be "D3DERR_DEVICELOST".
- DXGetErrorDescription9(hr) will return a string which describes the error code in hr. If hr were D3DERR_DEVICELOST, the string would be something like "The device has been lost but cannot be reset at this time. Therefore, rendering is not possible".
- DXTRACE_ERR("function�failed:�",�hr) will output - to the debug view - the current source file, line number, the string "function failed: " and the string produced by DXGetErrorString9(hr)
- DXTRACE_ERR_MSGBOX("function�failed:�",�hr) does exactly the same thing as DXTRACE_ERR, except that it pops up a dialog box with the error, as well as printing it to the debug output.
- DXTRACE_MSG("hello") is the same as writing DXTRACE_ERR("hello",�0). (It doesn't use hr at all but we're including it for completeness).
You can also feed the error code to the DirectX Error Lookup tool in the DirectX SDK to get a description.
When you're working with DirectX types in the debugger, you will probably find it useful to read In|Framez's article on auto-expand information for DirectX9. It'll make the debugger a lot more 'intelligent' when working with DirectX.

all the Macros above were can be found in DxErr.h

an example about this, the following code show the error message on a box

const WCHAR* errorString = DXGetErrorString(hr) ;
DXTRACE_ERR_MSGBOX(errorString, hr) ;

注意有些函数只能在DEBUG模式下使用

 

代码
#if defined(DEBUG) | defined(_DEBUG)
#define DXTRACE_MSG(str) DXTrace( __FILE__, (DWORD)__LINE__, 0, str, FALSE )
#define DXTRACE_ERR(str,hr) DXTrace( __FILE__, (DWORD)__LINE__, hr, str, FALSE )
#define DXTRACE_ERR_MSGBOX(str,hr) DXTrace( __FILE__, (DWORD)__LINE__, hr, str, TRUE )

 

 

 

posted on   翰墨小生  阅读(654)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2010年5月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
书法字典:https://www.shufadict.com
点击右上角即可分享
微信分享提示