For those of you writing Visual Studio.NET 2003 add-ins in C# (or any other .NET-enabled language), you might have noticed that CreateToolWindow doesn’t really work with real .NET controls.  Fortunately, this piece of code fixes this problem.  It’s a bit of hack, but it works for me so far and was created by using Reflector to figure out exactly what was going on.

// Run this code before any CreateToolWindow calls
Type tp = typeof( System.Windows.Forms.Control );
tp = tp.GetNestedType( "ActiveXImpl", BindingFlags.NonPublic );
FieldInfo fi = 
    tp.GetField( "globalActiveXCount", BindingFlags.Static | BindingFlags.NonPublic );
fi.SetValue( null, Convert.ToInt32( fi.GetValue( null ) ) + 1 );

I’m not sure if Craig Skibo would approve of this work-around, but it’s way easier (and cleaner, IMHO) than using that nasty shim control. If you want to figure out how this works, read my posting to the vsnetaddins Yahoo! group.

Read full post