none
How to get Exception Hresult Hex Code in .Net 4.0 RRS feed

  • Question

  • Hello,

    How can I extract the HResult in Hex Format as 0x8XXXXX in .Net 4.0 from catch(Exception e) ?

    Thursday, October 4, 2018 2:02 PM

All replies

  • Hi,

    upper or lower case? .ToString("X") or .ToString("x").

    Greetings, Chris

    Thursday, October 4, 2018 2:21 PM
  • But Enable to find Right HResult value as I get PasswordException 0x80070056 in catch(Exception e) 

    int Hresult = GetMarshal.GetHRForException(e);

    But In my case I am getting value -2146233087 which In hex results 7FECEAFF

    I want exact value 0x80070056 which I see in e.Message

    Thursday, October 4, 2018 2:26 PM
  • string str = "0x" + (-2146233087).ToString("X");

    I'm getting 0x80131501.

    Why not using e.HResult?

    Greetings, Chris


    • Edited by DerChris88 Thursday, October 4, 2018 3:02 PM
    Thursday, October 4, 2018 2:34 PM
  • But In e.Message I get Exception From HResult : 0x80070056)

    I want that value not 0x80131501

    Thursday, October 4, 2018 2:37 PM
  • 0x80070056 => -2,147,024,810

    Sincerely, Highly skilled coding monkey.


    Thursday, October 4, 2018 2:44 PM
  • Can you please post the complete exception you are getting? If there is an inner exception post this, too.

    Greetings, Chris

    Thursday, October 4, 2018 4:06 PM
  • int Hresult = GetMarshal.GetHRForException(e);

    Read this for potential problems with doing the above -

    https://blogs.msdn.microsoft.com/yizhang/2013/01/18/marshal-gethrforexception-does-more-than-just-get-hr-for-exception/

    The bottom line --

    "What if you really, really want to just get the HRESULT for a specific Exception? You should use Exception.HResult property. "

    • Edited by RLWA32 Thursday, October 4, 2018 4:21 PM
    Thursday, October 4, 2018 4:20 PM
  • Try this:

    string hResultHexString = "0x" + e.HResult.ToString("x");

    Greetings, Chris

    Thursday, October 4, 2018 4:41 PM
  • Are you saying that you are getting an exception that has a message like "Password Exception 0x12345" and when you get the HResult it is giving you 0xABCD AND you really want the code from the message?

    Most of the time the HResult on an exception is going to be the standard C++ exception code as it is often generating from code that happens to be written in C++. Windows errors will generally be the Win32 error code. For other .NET code exceptions it'll be whatever the developer set it to.

    Putting an error code in the exception message seems like a bad idea because of the very reason you posted this question. If I were to do that then I'd also expose the code as a property or via the HRESULT. Parsing strings shouldn't be necessary.

    But if you have to get the error out of the message and the HRESULT isn't the correct value then you have no choice but to parse it. Unfortunately unless you can narrow down the exception (e.g. of type PasswordException or something) then this is going to be error prone at best. Most exceptions won't have such a code so you'll just have to try and find it using a Regex expression or something. I don't know a better option here.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 4, 2018 5:56 PM
    Moderator
  •  

    Most of the time the HResult on an exception is going to be the standard C++ exception code as it is often generating from code that happens to be written in C++. 

    Michael Taylor 

    No, definitely not! HRESULT is not tied to any language. It is the result of an operation, which can be either successful or not.

    Definitely, it is not going to be the standard C++ exception as you wrote! 

     

    Windows errors will generally be the Win32 error code.  

    Michael Taylor 

    No. Windows API's results (errors and non-errors) are coded in HRESULT, which contains field specific for all Win32 errors.

    Please, do not post about C, C++, because you have already made clear you are not apt to explain anything about these languages.

    Also, include COM in the list... and extend this suggestion to your friends, who, just recently, wrote absurd concepts about COM.

    Thursday, October 4, 2018 9:01 PM
  • "Definitely, it is not going to be the standard C++ exception as you wrote! "

    Unhandled C++ exceptions use the HRESULT 0xE06D7363 as documented in this blog article from Microsoft. There are exceptions of course. That is how you can generally tell whether the exception was from C++ or not.

    "No. Windows API's results (errors and non-errors) are coded in HRESULT, which contains field specific for all Win32 errors."

    The standard Windows error codes are defined here. The comments mention how they are converted to HRESULTS in most cases. But you can just refer to the macro that is used to take an error code and convert to HRESULT. It is defined here. Or if you prefer the inverse.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 4, 2018 9:38 PM
    Moderator