SharpDX.Direct2D1.RenderTarget.EndDraw doesn't return HRESULT - how detect a failure? Especially D2DERR_RECREATE_TARGET?

佐手、 提交于 2019-12-12 04:43:31

问题


Porting code from Microsoft's old managed DirectX interfaces to SharpDX.

The documentation for Microsoft's ID2D1RenderTarget::EndDraw says that it should return an HRESULT:

If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code and sets tag1 and tag2 to the tags that were active when the error occurred.

Especially important is that this is the place I would usually detect the need to recreate the target (D2DERR_RECREATE_TARGET). Indeed, Microsoft shows this in their example:

hr = m_pRenderTarget->EndDraw();
if (hr == D2DERR_RECREATE_TARGET) ...

SharpDX's implementation is a subroutine - no return value. As seen in github source:

/// <returns>If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code and sets tag1 and tag2 to the tags that were active when the error occurred. </returns>
    public void EndDraw()

The comment still mentions the HRESULT, but this is void-returning.

How do I see the HRESULT?


回答1:


Examining SharpDX source:

    public void EndDraw(out long tag1, out long tag2)
    {
        SharpDX.Result result = TryEndDraw(out tag1, out tag2);
        result.CheckError();
    }

which calls:

    public void CheckError()
    {
        if (_code < 0)
        {
            throw new SharpDXException(this);
        }
    }

The exception constructor:

    public SharpDXException(Result result)
        : this(ResultDescriptor.Find(result))
    {
        HResult = (int)result;
    }

Sets this field of SharpDXException:

public int HResult { get; protected set; }

Therefore, SharpDX exposes the HRESULT by throwing a SharpDXException, with the HRESULT readable as ex.HResult, if you have a catch (SharpDXException ex) clause.


More info:
Convenience class to give names to the Direct2D exception HRESULTS.



来源:https://stackoverflow.com/questions/46610072/sharpdx-direct2d1-rendertarget-enddraw-doesnt-return-hresult-how-detect-a-fai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!