【问题标题】:is there any ATL supported macro that checks the hresults and returns the value?是否有任何 ATL 支持的宏来检查 hresults 并返回值?
【发布时间】:2012-06-04 11:31:39
【问题描述】:

我正在寻找与以下宏等效的东西:

#define FAILED_CHECK(hr) if(FAILED(hr)) return hr;

ATL 库中是否有任何实现如上所示的功能?

谢谢!

【问题讨论】:

    标签: c++ visual-c++ com error-handling atl


    【解决方案1】:

    无论出于何种原因,ATL 都没有像请求的那样提供非常有用的宏。从 Visual Studio 的几个版本开始,ATL 引入了类似的宏:

    /* Naming is slightly off in these macros
    ATLENSURE_RETURN(condition) is an HRESULT return of E_FAIL
    ATLENSURE_RETURN_VAL(condition, hr) is any return value (function can pick)
    ATLENSURE_RETURN_HR(condition, hr) is HRESULT-specific, though currently the same as _VAL
    */
    

    但再一次,没有立即匹配。你必须定义你自己的。或者,切换到在错误条件下抛出异常。 ATL 为此提供了有用的宏:ATLENSURE_THROW、ATLENSURE_SUCCEEDED 和朋友。

    特别是,您可以使用ATLENSURE_SUCCEEDED 在失败HRESULT 时抛出异常,然后捕获异常,尤其是。在从 COM 接口调用返回之前,将其转换回 HRESULT:

    _ATLTRY {
      // ...
      ATLENSURE_SUCCEEDED(pFoo->Bar()); // Bar returns HRESULT 
      // ...
    } _ATLCATCH(Exception) { // CAtlException class
      return Exception; // Back to HRESULT through CAtlException's operator
    }
    

    【讨论】:

    【解决方案2】:

    没有特定于 ATL 的宏,因为大多数 ATL 函数使用标准 HRESULT - 请参阅 most common HRESULT values 列表。

    使用最多的有macros for error handling,包括FAILED和SUCCEEDED:

    #define FAILED(hr) (((HRESULT)(hr)) < 0)
    #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
    

    所以您通常使用这些,或者在特定情况下您可能希望返回特定的 HRESULT,因此您最终会明确检查它是否等于您想要的值。在这种情况下,您很可能会以这样的方式结束:

    HRESULT hr = ...
    if (hr != S_OK)
        ...
    

    【讨论】:

      【解决方案3】:

      这是list of ATL macros sorted in alphabetical order,而不是这个列表集中在debugging and error reporting:看起来没有你要求的东西。

      【讨论】:

        猜你喜欢
        • 2013-06-04
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 2013-06-21
        • 2010-09-17
        相关资源
        最近更新 更多