## Tuesday, August 3, 2010

### COMExceptions, HRESULTS, and Windows Calculator

I was recently building a small web application in ASP.Net and C# where I had to deal with a little bit of ADSI and the System.DirectoryServices namespace. During the debugging phase, I used try/catch block to catch exceptions and present them in a label for development purposes and to create useful error messages for users for reasons that they would know and care about (such as an invalid username and password). One of the errors was a COMException with an ErrorCode property of 8007052E:
# as an HRESULT: Severity: FAILURE (1), Facility: 0x7, Code 0x52e
# for hex 0x52e / decimal 1326 :   ERROR_LOGON_FAILURE                                           winerror.h
# 1 matches found for "8007052e"

So, obviously I give the following a try in C# and am presented with a warning:

if (ex.ErrorCode == 0x8007052E)

Visual studio says "Comparison of integral constant is useless; the constant is outside the range of type 'int'". So I start working with the Exchange Error Code Look-up Tool  (note that it is not just for Exchange, but for any of the "19871 return codes registered from 172 sources" that it has archived on my system) and trial and error and do a little bit of truncation, looking up 0x7052E and 0x52E, which are presumably the same error code,

C:\>err 7052e
# as an HRESULT: Severity: SUCCESS (0), Facility: 0x7, Code 0x52e
# for hex 0x52e / decimal 1326 :   ERROR_LOGON_FAILURE                                           winerror.h
# 1 matches found for "7052e"

C:\>err 52e
# for hex 0x52e / decimal 1326 :   ERROR_LOGON_FAILURE                                           winerror.h