Error Message: External component has thrown an exception. Exception Type: System.Runtime.InteropServices.SEHException
I recently got a case that a .Net 1.1 windows application occasionally crashes while switching between forms. The error’s source is System.Windows.Forms and it is a SEHException type which has nothing to do with the software source code. The following shows the error description and the stack trace:
Message: External component has thrown an exception.
Exception Type: System.Runtime.InteropServices.SEHException
Occurred at Assembly: System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Stack: at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at … line 92
Further research on the web showed that we are not the only one facing the problem. Kevin Gearing commented on this issues as a confirmed bug in .Net 1.1. He found that this problem is related to Application.EnableVisualStyles().
I checked the maintenance history logs, and find out the EnableVisualStyles() added as part of recent change to the source code. Removing this function from application start up rectified the problem. So I concluded that using Application.EnableVisualStyles() introduced the error.
Now that we know what causes the error, the next question is that how to work around the problem, and have a XP style for .Net 1.1 application. I checked MSDN documentation on Application.EnableVisualStyles Method, and I noticed that there is a note about setting the FlatStyle property of the ButtonBase controls to FlatStyle.System. So I did as it is recommended, and unfortunately no effect on the problem. It seems using that the manifest is the only option. Enabling Visual Styles article in MSDN contains a detailed instruction on how to create a manifest for an application.