如何捕获来自.NET WebBrowser插件/加载项的异常?尤其是Flash



简短版本:

我有一个WinForms应用程序,它使用.NET 4.0 WebBrowser控件来显示包含Flash SWF文件的HTML页面。SWF文件是通过一些Javascript/jQuery/SWF)iobject魔术设置的。我会定期运行一个SWF文件,该文件会导致AccessViolationException,从而停止我的程序。据我所知,这种情况发生在显示一个页面和启动另一个页面之间的清理过程中(想想网站的自动幻灯片放映)。

我已经添加了AppDomain.CurrentDomain.UnhandledException和Application.ThreadException处理程序,我的程序仍然会在屏幕上显示一个对话框。我想防止对话框出现——如何捕捉这些类型的异常并抑制它们?我可以重新启动我的程序或其他什么-我不能显示对话框。

华丽细节:

我得到了上面提到的异常处理程序,并在通过InvokeScript()调用WebBrowser DOM中行为的地方添加了一些try/catch逻辑。但我所拥有的一切似乎都没有被召唤。

生成的异常如下所示:

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:UsersAUserAppDataLocalApps2.0AWMVPDR4.HEJZ9EP5M32.MQ4mmm...tion_2c82cc3ef3e7d3e9_0001.0000_6ffd6d2ca43477abMyProgram.exe
Faulting module path: C:Windowssystem32MacromedFlashFlash64_11_3_300_257.ocx
Report Id: a9f6977f-d284-11e1-a447-00187d1f4237

然后这个:

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()

我的挑战是阻止这个例外在屏幕上显示任何内容。

您可以从windows api中包含pinvoke方法,该方法可以抑制异常对话。

这是图书馆。http://www.pinvoke.net/default.aspx/kernel32.seterrormode

以下是用于此目的的c++文档。http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx

你把这个调用到你的代码中

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
}

OK-发现了一些关于.NET4的东西。这并不是什么新鲜事,对我来说只是新鲜事。非托管内容的异常处理与以前的.NET版本不同(我从来没有真正使用过,有点像.NET编程的新手)。以下链接有望提供一些指导和启示。。。

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

是否可以在.NET中捕获访问冲突异常?

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

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

有一些声明称,您可以向app.config中添加一个参数,并获得旧的异常处理行为。这对我不起作用,但我不会声称我做得很好。将decorator添加到Main()例程以启用try/catch行为的描述确实解决了我的问题。

最新更新