I just had a little brain freeze dealing with an HRESULT error from a COMException. When dealing with COM objects you still need to deal with the unfortunate COM idiosyncrasies like HRESULT values which get returned from just about every COM call.
COMException exposes this HRESULT as an int value in the Exception's ErrorCode property. Now in my brain freeze haze I tried something like this:
try
{
this.OutputBuffer = this.ComInstance.GetType().InvokeMember("ProcessHit", BindingFlags.InvokeMethod,
null, this.ComInstance,
new object[1] { this.CompleteInputBuffer }) as string;
}
catch (COMException ex)
{
// *** RPC Server Unavailable
if ( ex.ErrorCode == 0x800706ba )
{
// try to reload the server
if (this.LoadServer())
return this.CallComServer();
}
this.ErrorMessage = ex.Message;
return false;
}
catch (Exception ex)
{
this.ErrorMessage = ex.Message;
return false;
}
finally
{
this.EndRequest();
}
Now that seems like it should work well enough right?
Eh, not quite. The problem is that the constant hex value is created as a uint while the HRESULT is a signed integer. The code compiles just fine but at runtime the comparision may fail (depending on the HRESULT's actual value - high values like the one above (and aren't just about all COM errors 0x800... <s>) will fail.
So quick - how do you create a signed hex constant?
I don't know, but this works as expected:
if ( (uint) ex.ErrorCode == 0x800706ba )
Hopefully this will save somebody the 15 minutes I just wasted on this <s>...
Other Posts you might also like