Wednesday, January 21, 2009

Converting Com ErrorCodes to friendly messages

Using PerfMon to do some .NET profiling today, I wanted the ability to easily retarget my counters at a specific application running on my machine.

Everything went very well except for the error handling.  When adding my counters a  COMException may be thrown.  The Exception's message has the hex error code while the ErrorCode property has the value as an integer. I used the following code to convert the ErrorCode to be comparable against hex values provided by MSDN.

Update, an easier way to do this is comException.ErrorCode.ToString("X");


UInt32 errorCodeLookup = 0;
unchecked
{
    errorCodeLookup = (UInt32)comException.ErrorCode;
}

I didn't end up using this method as it was my own application.  I thought about using a uint enumeration to store all the error codes and then a dictionary to go lookup the user friendly error messages provided by MSDN, but since it is just a scrap app I didn't bother.

No comments: