2017-10-06 120 views
0

將微軟舊版託管DirectX接口的代碼移植到SharpDX上。SharpDX.Direct2D1.RenderTarget.EndDraw不返回HRESULT - 如何檢測失敗?特別是D2DERR_RECREATE_TARGET?

Microsoft's ID2D1RenderTarget::EndDraw的文件說,它應該返回一個HRESULT

如果該方法成功,則返回S_OK。否則,它將返回HRESULT錯誤代碼,並將tag1和tag2設置爲發生錯誤時處於活動狀態的標籤。

特別重要的是,這是我通常會檢測到需要重新創建目標的地方(D2DERR_RECREATE_TARGET)。事實上,微軟在他們的例子中顯示了這一點:

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

SharpDX的實現是一個子例程 - 沒有返回值。可以看出在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() 

評論還提到了HRESULT,但是這是void -returning。

如何查看HRESULT?

回答

0

檢查SharpDX來源:

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

的呼叫:

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

異常構造函數:

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

集SharpDXException的這個領域:

public int HResult { get; protected set; } 

因此,如果您有catch (SharpDXException ex)子句,則SharpDX通過拋出SharpDXException來暴露HRESULT,HRESULT可讀爲ex.HResult


更多信息:
Convenience class to give names to the Direct2D exception HRESULTS