问题
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