Warning! MAKE_HRESULT macro doesn't work!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Warning! MAKE_HRESULT macro doesn't work!

  1. #1
    Join Date
    Aug 1999
    Posts
    492

    Warning! MAKE_HRESULT macro doesn't work!

    I was using the SDK supplied "MAKE_HRESULT" macro to generate custom HRESULT values. Then I noticed that some of my values were getting duplicated and traced it back to the MAKE_RESULT macro having a serious flaw. The macro pieces together values using the << shift operator. This operator pushes bits off the edge causing duplicate HRESULTs to appear!


    First of all, an HRESULT is defined as a LONG, meaning it contains at most 32 bits.

    Here is how the MAKE_HRESULT macro works:
    Pass in 3 values and those are bit shifted into different sections of the HRESULT allowing you to make unique values. Here is the macro:


    #define MAKE_HRESULT(sev,fac,code)\
    ((HRESULT) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) \
    | ((unsigned long)(code))) )




    For the "Sev" value, you are the use a different value signifying the severity of the error:

    00 - Success
    01 - Informational
    10 - Warning
    11 - Error

    If you notice, the macro shifts the "Sev" value by 31 bits, not 30. This generates 2 problems:
    1) The upper bit of Warning and Error are lost
    2) Success and Warning have have the same value and Informational and Error have the same value.


    For example, say you define 2 HRESULTS:

    MY_INFO = MAKE_HRESULT(0x01, FACILITY_ITF, 5);
    MY_ERROR = MAKE_HRESULT(0x03, FACILITY_ITF, 5);
    // Note: 0x03 is 11 in binary




    The actual value of my 2 error codes should be different, but due to the bug in MAKE_HRESULT the values will be identical!

    Imagine if you were checking a function return for MY_INFO so you could treat it as a success, but the function actually returned MY_ERR!

    I'm planning on contacting Microsoft about this tomorrow because this is a serious issue and who knows how many app crashes this has generated.


    Why are the "tolerant" so easy to offend?

  2. #2
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Warning! MAKE_HRESULT macro doesn't work!

    Unlike NTSTATUS codes, HRESULTs have only two severity values:

    0 - success
    1 - failure

    so, MAKE_HRESULT definition is correct and the way you use it
    in MAKE_HRESULT(0x03, FACILITY_ITF, 5) is incorrect.

    Russian Software Development Network -- http://www.rsdn.ru

  3. #3
    Join Date
    Aug 1999
    Posts
    492

    Re: Warning! MAKE_HRESULT macro doesn't work!

    According to the documentation, the "sev" portion of the HRESULT can be one of 4 different values requiring 2 bits of data but only 1 bit is allowed.

    Either one thing is wrong: either an HRESULT can only be an error or success and the documentation is incorrect or the MAKE_HRESULT macro is incorrect.

    Why are the "tolerant" so easy to offend?

  4. #4
    Join Date
    Apr 2000
    Location
    San Francisco, California, USA
    Posts
    4,467

    Re: Warning! MAKE_HRESULT macro doesn't work!

    You are looking into wrong documentation. See this:
    http://msdn.microsoft.com/library/en...error_5g6r.asp



    Russian Software Development Network -- http://www.rsdn.ru

  5. #5
    Join Date
    Feb 2002
    Location
    OR, USA
    Posts
    2

    Re: Warning! MAKE_HRESULT macro doesn't work!


    MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x200+1)





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)