Was ist HRESULT?



  • In Windowsprogrammen taucht immer wieder eine long Variable Namens HRESULT auf. Was ist das genau? Wofür nutzt man die eigentlich? Was ist der Zweck?

    In den kleinen Applikationen, die ich bisher per MFC mit dem VS entwickelt hatte, brauchte ich mich nie drum scheren, aber gerade gehe ich andere Windowsprogrammbeispiele durch und irgendwie werde ich aus dem Sinn der Verwendung von HERSULT nicht ganz schlau.



  • What is an HRESULT?
    
    COM component methods use HRESULTs to report error/success conditions to their users. For example, methods like CoCreateInstance( ), QueryInterface( ), CLSIDFromProgID( ), all return an HRESULT. Most COM interface methods return a HRESULT. While the name HRESULT might make you think an HRESULT is a handle to a result. it isn’t. A HRESULT is a 32-bit value divided into three different fields. The left-most bit reports the success/failure of the method. The right-most 16 bits contain the return code (reason for success/failure). The middle 15 bits provide more information about the type and the origin of the return code. This is often known as the facility code.
    
    By convention successful return codes begin with an S_, while failure codes begin with an E_. Meanings of some of the commonly used return codes are given below.
    
    Name
    
    Meaning
    
    S_OK
    
    Function succeeded and is returning a Boolean true. S_OK is defined as 0.
    
    S_FALSE
    
    Function succeeded and is returning a Boolean false. S_FALSE is defined as 1.
    
    E_UNEXPECTED
    
    Unexpected failure.
    
    E_NOTIMPL
    
    Member function is not implemented.
    
    E_NOINTERFACE
    
    Component does not support requested interface.
    
    E_OUTOFMEMORY
    
    Component could not allocate required memory.
    
    E_FAIL
    
    Unspecified failure.
    
    Note that S_FALSE is defined as 1 and S_OK is defined a 0. These definitions are contrary to traditional C/C++ programming principles, according to which o is false and nonzero is true. Therefore, when you use HRESULTs be sure you explicitly compare the return values to S_FALSE or S_OK.
    
    The facility code identifies the part of the operating system that can return the return code. The currently defined facility codes are listed below:
    
        *
    
          FACILITY_WINDOWS 8
        *
    
          FACILITY_STORAGE 3
        *
    
          FACILITY_SSPI 9
        *
    
          FACILITY_RPC 1
        *
    
          FACILITY_WIN32 7
        *
    
          FACILITY_CONTROL 10
        *
    
          FACILITY_NULL 0
        *
    
          FACILITY_ITF 4
        *
    
          FACILITY_DISPATCH 2
        *
    
          FACILITY_CART 11 
    
    If one team of Microsoft developers decide to return a return code of 2 to signify some error condition, there is a possibility that another team too decides to return a return code of 2, this time signifying a totally different error condition. This would lead to a conflict. This conflict is sorted out through the facility codes. One team can return a 2 in return code so long as each of them returns a different facility code.
    
    To determine the facility of an HRESULT use the macro HRESULT_FACILITY defined in WINERROR.H. To determine whether the method returned success or failure we can use the macros FAILED and SUCCEEDED.
    


  • Also ist folgendes so aufzufassen:

    if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
            return hr;
    

    "Wenn die Methode Create Device nicht das tut, was sie soll, wird ein Wert für hr (ein HRESULT Typ) erzeugt und zurückgegebn, der dann diesen- ich nenn es jetzt mal HRESULT Errorcode- zurückgibt"

    Oder doch was anderes?



  • HRESULT könnte man mit einem flexiblen bool Wert vergleichen, die meisten Funktionen liefern ok(true)/error(false) es giebt aber reichlich ausnahmezustände um die Fehlerart sofort zu erfassen, was auch nicht immer ein Fehler sein muß, sondern als reine Benachrichtigung diennt.
    Wird ein Error zurückgegeben, kann man die fehlermeldung mit GetLastError() abfragen, Meldungen die darüber nicht gespeichert werden, werden direkt ausgegeben, sofern es keine reine Fehler sind.


Anmelden zum Antworten