複製鏈接
請複製以下鏈接發送給好友

HRESULT

鎖定
HRESULT即函數返回值, 是一種簡單的數據類型,通常被屬性和 ATL 用作返回值。
外文名
HRESULT
性    質
科學
類    別
計算機學
A value returned from a function call to an interface, consisting of a severity code, context information, a facility code, and a status code that describes the result. For 16-bit Windows systems, the HRESULT is an opaque result handle defined to be zero for a successful return from a function, and nonzero if error or status information is to be returned. To convert an HRESULT into a more detailed SCODE (or return value), applications call GetSCode(). See SCODE.
如果這個函數是執行完返回的話將包含具有實際意義的數據,如果立即返回則包含狀態信息--發送成功與否,並不能説明執行的如何。下表説明各種不同的值。頭文件 winerror.h 中包含更多的值。
名稱 説明 值
S_OK 操作成功 0x00000000
E_UNEXPECTED 意外的失敗 0x8000FFFF
E_NOTIMPL 未實現 0x80004001
E_OUTOFMEMORY 未能分配所需的內存 0x8007000E
E_INVALIDARG 一個或多個參數無效 0x80070057
E_NOINTERFACE 不支持此接口 0x80004002
E_POINTER 無效指針 0x80004003
E_HANDLE 無效句柄 0x80070006
E_ABORT 操作已中止 0x80004004
E_FAIL 未指定的失敗 0x80004005
E_ACCESSDENIED 一般的訪問被拒絕錯誤 0x80070005
不能簡單地把返回值與 S_OK 和 E_FAIL 比較,而要用 SUCCEEDED 和 FAILED 宏進行判斷。
HRESULT 其實是一個32位的值,其最高位(bit)如果是0表示成功,1表示錯誤。具體參見 MSDN 之"Structure of COM Error Codes"説明。我們在程序中如果需要判斷返回值,則可以使用比較運算符號;switch開關語句;也可以使用VC提供的宏: HRESULT hr = 調用組件函數;
if( SUCCEEDED( hr ) ){...} // 如果成功
......
if( FAILED( hr ) ){...} // 如果失敗
......