0

There are three different return/error/status code types I get while calling native function using P/Invoke.

  1. The first one is the usual error code I get by calling Marshal.GetLastWin32Error static method.

  2. The second one is HRESULT I get while calling some COM-helper functions.

  3. And the third one is NTSTATUS I get while calling native apis from ntdll.dll library. I have a loging function that logs the return/error/status code as a hexidecimal number, then when an error is found in the logs I need to use google to find out what the actual error is.

So I'd like to ask if there is a good way to convert these return/error/status codes to string (preferably in English) to log the return/error/status string description instead of a number?

Is there a way to convert between LastError/HRESULT/NTSTATUS codes, so that the semantics of the error message would stay the same?

kennyzx
  • 12,845
  • 6
  • 39
  • 83
user2102508
  • 1,009
  • 11
  • 23
  • 1
    Not treating these errors as exceptional is pretty problematic, nothing ever nice happened and the odds you can just shrug them off ought to be quite low. Do consider throwing an exception, you can always log in a catch handler. Win32 exceptions are generated by Win32Exception, no need to call GetLastWin32Error(). COM exceptions are generated with Marshal.ThrowExceptionForHR(). They both do the work of getting a textual representation for the error. NTSTATUS codes are tricky since they are often generated by a specific module, you'll have to pinvoke FormatMessage(). – Hans Passant Nov 20 '17 at 11:08
  • There's no magic bullet IMHO. FormatMessage is often useful as explained here on SO: https://stackoverflow.com/questions/7008047/is-there-a-way-to-get-the-string-representation-of-hresult-value-using-win-api otherwise I suggest this online free tool (that I wrote recently afters 20+ years having the same issue as you) that's much better than random googling: http://www.magnumdb.com it will get you error code by name or name by code, etc. and the .h file that defines it, etc. – Simon Mourier Nov 20 '17 at 11:29

1 Answers1

0

You can use errorlook tools for this purpose. This link is kind of old, I learned this in my old VC++ days and you can use it as a start point to find modern tools.

As for the logging I would suggest you keep the original error codes. Meaningful error messages are easier to read, but the original error codes are accurate keywords to the search engines.

kennyzx
  • 12,845
  • 6
  • 39
  • 83