C# Web 浏览器控制 System.AccessViolationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/178898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:47:26  来源:igfitidea点击:

C# WebBrowser Control System.AccessViolationException

c#.netexceptionbrowser

提问by Aaron Smith

I have a program that uses the built in webbrowser control. At some point during the usage of this, I'm not sure at what point, but it appears to be random, I get the following error:

我有一个使用内置 webbrowser 控件的程序。在使用它的某个时候,我不确定在什么时候,但它似乎是随机的,我收到以下错误:

System.AccessViolationException

FullText = System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)

Does anyone have any clues as to why I would get this and how to prevent it?

有没有人知道为什么我会得到这个以及如何防止它?

回答by Jeff Yates

My gut feeling is that you are trying to manipulate the document before you've navigated to one. Try navigating to "about:blank" before changing the document text or html.

我的直觉是,您在导航到某个文档之前就试图操作该文档。在更改文档文本或 html 之前尝试导航到“about:blank”。

If you already are performing navigation, note that navigation is asynchronous, so you need to monitor the events of the browser in order to detect when the navigation is complete. Otherwise, you may try to write to the document before it exists.

如果您已经在执行导航,请注意导航是异步的,因此您需要监视浏览器的事件以检测导航何时完成。否则,您可能会尝试在文档存在之前写入该文档。

回答by Judah Gabriel Himango

We're hitting this too. Inconsistently, we'll get this exception.

我们也在打这个。不一致的是,我们会得到这个例外。

Some questions to help narrow this down: are you using any mshtml interfaces directly (e.g. mshtml.dll)? Doing any COM interop directly?

一些有助于缩小范围的问题:您是否直接使用任何 mshtml 接口(例如 mshtml.dll)?直接进行任何 COM 互操作?

We've found that calling some of the COM MSHTML interfaces incorrectly can cause this.

我们发现错误地调用某些 COM MSHTML 接口会导致这种情况。

We've also found that doing COM marshalling incorrectly can cause this.

我们还发现,不正确地进行 COM 编组会导致这种情况。

If there's a bug in the MSHTML interface import that the built-in WebBrowser uses, it can cause this.

如果内置 WebBrowser 使用的 MSHTML 界面导入中存在错误,则可能导致此问题。

Accessing document IFRAME Elements from another domain can cause this.

从另一个域访问文档 IFRAME 元素可能会导致这种情况。

It's possible that making WebBrowser calls when the document isn't quite ready may also cause this.

在文档还没有完全准备好时进行 WebBrowser 调用也可能导致这种情况。

回答by Kevin Dark

Just a suggestion, I'm no expert on this but have used the WebBrowser a lot in previous applications, but why dont you write a function to wait 1 second before attempting to pass the browser anything and always check the readystate beforehand aswell. Might slow it down a bit but it should make it bullet proof. :)

只是一个建议,我不是这方面的专家,但在以前的应用程序中使用了很多 WebBrowser,但是为什么不编写一个函数来等待 1 秒,然后再尝试通过浏览器传递任何内容,并始终事先检查就绪状态。可能会慢一点,但它应该使它防弹。:)

回答by Bob King

Are the pages you are navigating to hosting any ActiveX controls? If yes, one of those may be flawed. Also check your pages in IE. See if they crash the same way. That will help isolate if it's specific to the content or the browser control.

您要导航到的页面是否包含任何 ActiveX 控件?如果是,其中之一可能存在缺陷。还要检查您在 IE 中的页面。看看它们是否以同样的方式崩溃。这将有助于隔离它是否特定于内容或浏览器控件。

回答by Aaron Smith

I ended up just opening up the webpage in the browser. That way I don't even have to worry about this. It's still strange that it throws this error though.

我最终只是在浏览器中打开了网页。这样我就不用担心这个了。尽管如此,它仍然抛出这个错误仍然很奇怪。

回答by Darkmage

This seems to be a Vista Issue, what happend to me was that my C# webBrowser1 opend a web page that runned a java applet that opend a external IE webpage that runs a ActiveX app/script.

这似乎是一个 Vista 问题,发生在我身上的是我的 C# webBrowser1 打开了一个运行 Java 小程序的网页,该小程序打开了一个运行 ActiveX 应用程序/脚本的外部 IE 网页。

When the ActiveX script tryes to update back in to the memory of the C# app the DEP "Data Execution Prevention" in Vista flags this operation as hostile/virus and ends the program with the System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

当 ActiveX 脚本尝试更新回 C# 应用程序的内存时,Vista 中的 DEP“数据执行保护”将此操作标记为恶意/病毒,并以 System.AccessViolationException: Attempted to read or write protected memory 结束程序。这通常表明其他内存已损坏。”

My fix for this was to turn of DEP in Vista with this line in cmd

我对此的解决方法是在 cmd 中使用这一行在 Vista 中关闭 DEP

"bcdedit.exe /set {current} nx AlwaysOff"

and reboot the machine.

并重新启动机器。

XP also run DEP so in certain cases i guess this cold happen here too. To test if its a DEP issue do this.

XP 也运行 DEP,所以在某些情况下,我猜这种感冒也会发生在这里。要测试它是否是 DEP 问题,请执行此操作。

Right click on "My Computer" Select "Properties" and "Advanced" Under "Startup and Recovery, click Settings Now click on "Edit" The notepad has just begun. Simply replace the line: Code: noexecute optionn by AlwaysOff Restart your PC to complete the transaction.

右键单击“我的电脑”选择“属性”和“高级”在“启动和恢复”下,单击“设置”现在单击“编辑”记事本刚刚开始。只需替换以下行:代码:noexecute optionn by AlwaysOff Restart your PC to完成交易。

If you want to reactivate the DEP be sufficient to conduct the reverse, like this:

如果您想重新激活 DEP 就足以进行反向操作,如下所示:

Replace by Quote: AlwaysOff noexecute = noexecute = optin

替换为引用:AlwaysOff noexecute = noexecute = optin

回答by Filip Navara

We have recently had similar problem on machines of several customers. The problem turned out to be a bug in the MSHTML control in certain environments. A common symptom for the problem seems to be broken registration of the jscript.dll library.

我们最近在几个客户的机器上遇到了类似的问题。结果证明该问题是某些环境中 MSHTML 控件中的错误。该问题的一个常见症状似乎是 jscript.dll 库的注册中断。

Symptoms that may help to diagnose if it's the same problem - the jscript.dll is not listed in Modules in the debugger and is not loaded by the process; Native stack trace for the crash is the following:

可能有助于诊断是否是同一问题的症状 - jscript.dll 未在调试器的模块中列出,也未由进程加载;崩溃的本机堆栈跟踪如下:

mshtml!CRootTracker::CollectGarbageInternal+0xd
mshtml!CDoc::ReduceMemoryPressureTask+0x29
mshtml!CStackPtrAry<unsigned long,12>::GetStackSize+0xb6
mshtml!GlobalWndProc+0x183
USER32!InternalCallWinProc+0x23
USER32!UserCallWinProcCheckWow+0x109
USER32!DispatchMessageWorker+0x3bc
USER32!DispatchMessageW+0xf

The solution is to re-register the jscript.dll library and the crash should go away.

解决方案是重新注册 jscript.dll 库,崩溃应该会消失。

Re-registering the library is done as follows (example given for 64-bit Windows, otherwise only the first line is necessary):

重新注册库的操作如下(以 64 位 Windows 为例,否则只需要第一行):

C:\Windows\System32\regsvr32.exe C:\Windows\System32\jscript.dll
C:\Windows\SysWOW64\regsvr32.exe C:\Windows\SysWOW64\jscript.dll

Both commands have to be "Run as Administrator".

这两个命令都必须是“以管理员身份运行”。

回答by Jacotb

I encountered this exception at various occasions while trying to access WebBrowser.ReadyState and WebBrowser.Document.

在尝试访问 WebBrowser.ReadyState 和 WebBrowser.Document 时,我在各种场合遇到了这个异常。

I was having the exceptions exclusively on Windows XP 32bit. After the other solutions didn't help, it appeared to be a threading issue. I surrounded any code blocks that accessed the web browser control with mutex locks, and that seemed to solve the problem.

我在 Windows XP 32 位上有例外。在其他解决方案没有帮助之后,它似乎是一个线程问题。我用互斥锁包围了访问 Web 浏览器控件的所有代码块,这似乎解决了问题。