Author Archive

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.

How to avoid Visual Studio Help

For what seems like the thirteen-thousandth time, I just accidentally pushed the F1 key while I was writing some code. It’s pretty close to the escape key. I didn’t mean to push it. I guess I just have fat fingers.

I really, really hate pressing F1 in Visual Studio. Usually, it takes about a minute to display Microsoft’s help documentation thingy, which is impossible to navigate, frequently wrong and and generally not very helpful. This afternoon, the document explorer decided it had to go and update itself, which took about five minutes before it could take it’s usual minute to load the non-relevant, non-help, that I didn’t even want in the first place!

During this time, Visual Studio was COMPLETELY Unusable. The help dialog blocks the main visual studio  thread - and all attempts to get back to work were greeted with a friendly, informative “This may take several minutes” dialog.

Time Passes…
Time Passes…
Time Passes…

Arggh! Gord Mad!… And it turns out it’s not just me. This annoys other folks, too!

Right. That’s it Visual Studio. You’ve made me go through this song and dance for THE LAST TIME!

For starters, where do we all go for help? To Google, that’s where. So, I added an external tool using the Tools>External Tools Method:

Adding an External Tool

I set up my command to point to Firefox, and passed as the arguments:

http://www.google.com/search?site=&hl=en&q=$(CurText)+c%23&

(The +c%23& part of the command appends “C#” to whatever is highlighted in the IDE. If you’re not using C#, you could leave it out, or substitute it with whatever else you usually search for)

Then, I flipped over to the Keyboard bindings screen (Tools > Options > Keyboard:)

VS 2008 Keybinding

VS 2008 Keyboard Binding Screen

And I re-mapped the F1 key to my new ExternalCommand1.

There! Now, whenever I press F1, Visual Studio opens a new tab on my web browser, and searches Google for whatever I have highlighted in the IDE.

Purposefully punishing developers with a minute or two wait everytime they press a certain key is just plain unforgivable. They get really distracted trying to work around the “functionality”, and then further distracted writing ranty blog posts about it…

P/Invoke Hooray();

The BCLTeam just released a really neat looking tool that makes it much easier for us Managed Code types to interoperate with the dirty bowels of windows (also known as the Win32 API : )

Called the P/Invoke Generator, it inspects any Win32 API call, and then generates the interop code that you need to be able to call it in either C# or VB.Net. It also lets you generate P/Invoke calls from any other native library.

Nice one! You can download the tool for zero dollars over on CodePlex

WCF, WebHttp Binding, and Authentication

If you’re trying to build REST-enabled services with WCF, you’ll want to use the webHTTP Binding.

This binding defaults to anonymous handling, so if you’re planning on doing any authorization you need to change the binding configuration.

This took me ages to figure out, largely because the webHttp binding is new and not well documented.

To change the binding to support NTLM or Windows authentication, add the following node to the system.servicemodel in your app.config:

  1. <bindings>
  2. <webHttpBinding>
  3. <binding name="varkBinding">
  4. <security mode="TransportCredentialOnly">
  5. <transport clientCredentialType="Ntlm"  />
  6. </security>
  7. </binding>
  8. </webHttpBinding>
  9. </bindings>

Note that you can supply one of five values to the mode attribute: None, Basic, Digest Windows, NTLM, and Certificate.

Once you’ve picked the one you want, in your service definition, specify your new binding from the bindingConfiguration attribute:

  1. <service behaviorConfiguration="SyndicationBehavior"
  2. name="Yourapp.Yourservice">
  3. <endpoint address="http://localhost:8000/url"
  4. behaviorConfiguration="SomeBehavior"
  5. binding="webHttpBinding" bindingConfiguration="varkBinding"
  6. contract="YourContract.IYourService" />
  7. </service>

And all calls to your REST service should be made with a valid windows network identity.

In your serviceCode, you can retrieve it from the System.ServiceModel namespace:

  1. ServiceSecurityContext.Current.WindowsIdentity

Oh, and if you’re not comfortable with editing the XML files, or you’d like to explore the dizzying array of other available options in WCF, I also discovered the Microsoft Service Configuration Editor (possibly someone had discovered it before me. ) It lives at C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcConfigEditor.exe. It gives you a simple(ish) visual way to edit WCF Configuration files.

UPDATE: JustinJSmith over at The Cybertopian Chronicle points out that it’s even easier to invoke the config editor - you just right click on your app.config, and select “Edit WCF Configuration” (duh…)

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!