Archive for New

Handling COM Error Codes

Sometimes COM Objects return a HRESULT that is outside of the bounds of a C#  integer.

Unfortunately, the only way to correctly handle a COM Exception in .NET is to use the ComException Class. But if the HRESULT isn’t an Int, and the ErrorCode Property on the ComException class is an Int, how are you supposed to ever be able to catch that specific HRESULT?

Fear not! Here’s how to handle a COM ErrorCode that can’t be converted to an Int (in this example, it’s unsigned)

try
  1.  {
  2.   _PropertyHandler.Open(filePath, false, dsoFileOpenOptions.dsoOptionDefault);
  3.  }
  4.  catch (COMException comEx)
  5.  {
  6.   if (comEx.ErrorCode == unchecked((int)0×800300FC))
  7.    {
  8.     throw new FileNotFoundException("Could not find file:");
  9.    }
  10.  }

The magic happens in the ‘unchecked’ statement - which tells the compiler not to perform the overflow-checking context for integral-type arithmetic operations and conversions.

More on the Unchecked Operator over on MSDN.

Welcome Underground

Thanks for stopping by!

The Infovark Underground is a new blog where Dean and I can unleash our inner nerd, and share some of the technology and experiences we run into as we build Infovark (Yes, we call our product infovark. It’s got the same name as our company, because we’re all about making things easy to remember and share..)

Unlike our Infovark blog, which details what we’re doing, the underground will get into much more technical detail about how we’re doing it - discussing programming, development and tools.

If that sounds like the kind of stuff you might be interested in, feel free to add our feed to your readers!