9/15/2010

Office Add-In and Screen Refresh Issues

When I recently developed an Office add-in (to be more specific: Outlook 2003) I came across a re-draw issue – but only on a few machines.

When reducing the color depth, the issue would go away – sometimes!

I searched for solutions (or even the cause of the problem) for weeks! Finally I had a machine that crashed completely as soon as the form was to be displayed, and this helped me a lot.

This was the exception text:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)

Now I had something to search on and found out that this issue was first reported in 2005 (!) and has apparently not been fixed my MS since.

For me the problem only comes up with MS Office add-ins and when using a DataGridView. Luckily there is a workaround: if you tell the DataGridView to use double-buffering, the issue goes away, and at the same time scrolling in the grid is much faster (so you may even want to do this if you don’t experience the problem).

Unfortunately the DoubleBuffered attribute of the DataGridView is not exposed, so you need to sub-class DataGridView or use reflection to set it. I felt that the latter approach is less trouble. Here’s a nice example to use ExtensionMethods in VS 2010 to set the attribute.

Since then the problem has gone away completely for all of my users.

Thanks to MS for having wasted the better part of two weeks of my time by not fixing a bug that they have known of for five years.

No comments:

adaxas Web Directory