1

Short Version:

I have a WinForms app that uses a .NET 4.0 WebBrowser control to display an HTML page containing a Flash SWF file. The SWF file gets set up via some Javascript/jQuery/swf)iobject magic. Periodically, I run across a SWF file that will cause an AccessViolationException that stops my program. From what I can tell, this happens during the cleanup between showing one page and starting another (think automatic slideshow of web sites).

I've added AppDomain.CurrentDomain.UnhandledException and Application.ThreadException handlers, and my program still stops with a dialog on the screen. I want to prevent the dialog from showing up - how do I catch these types of exceptions and suppress them? I can restart my program or something - I can't have a dialog show up.

Gory Details:

I've got the exception handlers mentioned above, plust some try/catch logic around places where I'm invoking behavior in my WebBrowser DOM via InvokeScript(). But nothing I've got in place seems to get called.

The exception generated looks like this:

Faulting application name: MyProgram.exe, version: 1.0.0.0, time stamp: 0x50096c59
Faulting module name: Flash64_11_3_300_257.ocx, version: 11.3.300.257, time stamp: 0x4fc81d71
Exception code: 0xc0000005
Fault offset: 0x000000000022b1db
Faulting process id: 0x10d0
Faulting application start time: 0x01cd668d0db086f8
Faulting application path: C:\Users\AUser\AppData\Local\Apps\2.0\AWMVPDR4.HEJ\Z9EP5M32.MQ4\mmm...tion_2c82cc3ef3e7d3e9_0001.0000_6ffd6d2ca43477ab\MyProgram.exe
Faulting module path: C:\Windows\system32\Macromed\Flash\Flash64_11_3_300_257.ocx
Report Id: a9f6977f-d284-11e1-a447-00187d1f4237

and then this:

Application: MyProgram.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at System.Windows.Forms.UnsafeNativeMethods+IDispatch.GetIDsOfNames(System.Guid ByRef, System.String[], Int32, Int32, Int32[])
   at System.Windows.Forms.HtmlDocument.InvokeScript(System.String, System.Object[])
   at MyProgram.InvokeScriptExtension+<>c__DisplayClass2.<InvokeScript>b__0()
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.WebBrowserBase.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.WebBrowser.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
   at MyProgram.Program.Main()

My challenge is to stop that exception from putting anything on the screen.

GuyWithDogs
  • 683
  • 1
  • 4
  • 18
  • As an update, I have the following lines as the new start of my Main() routine. I had them in a different spot and in a slightly different order earlier and STILL got the exception dialog, so here's my current incantation: Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); – GuyWithDogs Jul 25 '12 at 21:00
  • I have the same Problem, would love to know if you ever found an answer to this. Just to add, I changed from web browser control to a axshockwave control with the same problems. – SmithMart Aug 06 '12 at 14:12

2 Answers2

1

You can include the pinvoke method from the windows api that suppresses exception dialogues.

Here is the library for that. http://www.pinvoke.net/default.aspx/kernel32.seterrormode

Here is the c++ documentation for that. http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx

You call this into your code

class MainClass
{
[DllImport("kernel32.dll")]
static extern ErrorModes SetErrorMode(ErrorModes uMode);

[Flags]
public enum ErrorModes : uint
{
    SYSTEM_DEFAULT = 0x0,
    SEM_FAILCRITICALERRORS = 0x0001,
    SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
    SEM_NOGPFAULTERRORBOX = 0x0002,
    SEM_NOOPENFILEERRORBOX = 0x8000
}
[System.STAThreadAttribute()]
static void Main()
{
    SetErrorMode(ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);
  // this function prevents error dialog box to show up after application crash
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Progdom
  • 91
  • 7
0

OK - found something out about .NET 4. It wasn't new, just new to me. Exception handling for unmanaged stuff works differently than it did in previous .NET versions (which I never really used, being somewhat of a newbie to .NET programming). The following links will hopefully provide some guidance and illumination...

http://msdn.microsoft.com/en-us/library/dd638517.aspx

Is it possible to catch an access violation exception in .NET?

http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

There's some claims that you can add a parameter to the app.config and get the old exception handling behavior. That didn't work for me, but I won't claim I did it properly. The description of adding the decorator to the Main() routine to enable the try/catch behavior did solve my problem.

Community
  • 1
  • 1
GuyWithDogs
  • 683
  • 1
  • 4
  • 18