HRESULT 가 뭐냐?

출처 : http://blog.naver.com/hopal78/100013977424

HRESULT

The HRESULT data type is a 32-bit value that is used to describe an error or warning.

typedef LONG HRESULT; 

Remarks

On 32-bit platforms, the HRESULT data type is the same as the SCODE data type. On 16-bit platforms, an SCODE value is used to generate an HRESULT value.

An HRESULT value is made up of the following fields:

  • A 1-bit code indicating severity, where zero represents success and 1 represents failure.
  • A 4-bit reserved value.
  • An 11-bit code indicating responsibility for the error or warning, also known as a facility code.
  • A 16-bit code describing the error or warning.

========================================================================================

아래는 winerror.h에 나온 SCODE, HRESULT내용 입니다

퍼실리티(Facility)와 리턴코드(Return Code)에 관련된 상태코드에 대한 내용은 <winerror.h>에 정의되어 있으며

========================================================================================

////////////////////////////////////
//                                                //
//     OLE Error Codes                   //
//                                                //
////////////////////////////////////

//
// OLE error definitions and values
//
// The return value of OLE APIs and methods is an HRESULT.
// This is not a handle to anything, but is merely a 32-bit value
// with several fields encoded in the value.  The parts of an
// HRESULT are shown below.
//
// Many of the macros and functions below were orginally defined to
// operate on SCODEs.  SCODEs are no longer used.  The macros are
// still present for compatibility and easy porting of Win16 code.
// Newly written code should use the HRESULT macros and functions.
//

//
//  HRESULTs are 32 bit values layed out as follows:
//
//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
//  +-+-+-+-+-+---------------------+-------------------------------+
//  |S|R|C|N|r|    Facility         |               Code            |
//  +-+-+-+-+-+---------------------+-------------------------------+
//
//  where
//
//      S - Severity - indicates success/fail
//
//          0 - Success
//          1 - Fail (COERROR)
//
//      R - reserved portion of the facility code, corresponds to NT's
//              second severity bit.
//
//      C - reserved portion of the facility code, corresponds to NT's
//              C field.
//
//      N - reserved portion of the facility code. Used to indicate a
//              mapped NT status value.
//
//      r - reserved portion of the facility code. Reserved for internal
//              use. Used to indicate HRESULT values that are not status
//              values, but are instead message ids for display strings.
//
//      Facility - is the facility code
//
//      Code - is the facility's status code
//

//
// Severity values
//



번역


HRESULT 형의 구조
0~15 비트 : RETURN CODE
16~28 비트 : FACILITY
29~30 비트 : R
31 비트 : SEVERITY

SEVERITY(31비트)는 함수의 수행의 성공과 실패를 알려주는 코드다. 를 검사하기 위해서는 다음 예제코드와 같이 SUCCEEDED 혹은 FAILED 매크로를 사용해야 한다.

//SUCCEEDED 경우
   ICalc* pICalc = NULL;

   HRESULT hr = pI->QueryInterface(IID_ICalc, (void**)&pICalc);
   if (SUCCEEDED(hr)) {
       pICalc->Add();
   }
   pICalc->Release();

//FAILED 경우
   HRESULT hr;
   hr = ::CoInitialize(NULL);
   if(FAILED(hr)) {
       AfxMessageBox("COM 라이브러리를 초기화할 수 없다.");
       return FALSE;
   }

< 상태코드 요약 >
S_OK : 때때로 Boolean TRUE 값(0X0)으로 S_FALSE와 함께 사용되며 함수가 성공하였음을 의미한다.

NOERROR : S_OK와 동일한 의미이다.

S_FALSE : S_OK와 반대로 Boolean FALSE 값(0X1)으로 함수가 실폐하였음을 의미한다.

E_UNEXPRCTED : 치명적인 실패를 의미한다.

E_NOTIMPL : 멤버 함수에 구현 코드가 포함되어 있지 않다.

E_OUTOFMEMORY : 필요한 메모리를 할당할 수 없음

E_INVALIDARG : 하나 혹은 그 이상의 인자가 타당하지 않음

E_NOINTERFACE : 요청한 인터페이스를 지원하지 않음

E_POINTER : 타당하지 않은 포인터

E_HANDLE : 타당하지 않은 처리

E_ABORT : 작동 중지

E_FAIL : 특정하지 않은 실패

E_ACCESSDENIED : 일반적 접근이 금지된 에러


///////////////////////////////////////////////////////////////////////

5.2.2 HRESULT

COM 인터페이스에 소속된 대부분의 함수들은 HRESULT형의 값을 반환하고 있는 데, 멤버 함수들이 이러한 HRESULT형의 값을 반환하는 것은 COM의 규정에 의한 것은 아니다.

그러나 이와 같이 COM 인터페이스에 소속된 대다수 멤버 함수들이 HRESULT형을 반환하면, 클라이언트에게 컴포넌트의 멤버 함수들의 수행과 관련한 상태정보를 일관된 방법으로 반환하게 해주는 결과가 될 것이며, 또한 COM의 하부 시스템에서 발생하는 오류들에 대한 정보를 전달할 수 있게 한다.

즉 COM의 하부 시스템이 아파트먼트(Apartment), 프로세스, 시스템 등의 경계를 가로질러 구성되면, 이러한 하부 시스템은 멤버 함수의 호출에 개입하게 된다. 이때 멤버 함수의 반환 자료형으로 HRESULT형을 사용하면 하부 시스템에서 발생하는 오류들에 대한 정보들을 호출자 즉 클라이언트에게 전달해 줄 수 있다.

2006/10/23 23:12 2006/10/23 23:12

글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다

덧글을 달아 주세요