WinForms WebBrowser HtmlDocument.Write behaves differently in different solutions

0

Recently, my application has crashed when trying to display a rather lengthy (but otherwise simple) HTML e-mail. The crash was caused by mshtml.dll getting a stack overflow (exception code 0xc00000fd). Of note here is that this didn't throw an exception but it actually just crashed the program as a whole. The error was retrieved from the Windows event log.

In the process of debugging, I created a smaller sample solution to try and narrow down the issue. However, not only does it work fine in the sample solution, it behaves completely different from the main program despite running the same code even for the simplest of HTML strings.

The code is as follows:

var webBrowser1 = new System.Windows.Forms.WebBrowser();
webBrowser1.AllowNavigation = false;
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.Navigate("about:blank");

var doc = webBrowser1.Document.OpenNew(true);
doc.Write("<HTML><BODY>This is a new HTML document.</BODY></HTML>");
var count = doc.All.Count;
var html = doc.All[0].OuterHtml;

In the sample solution this evaluates to:

count = 4; // [HTML, HEAD, TITLE, BODY]
html = "<HTML><HEAD></HEAD>\r\n<BODY>This is a new HTML document.</BODY></HTML>";

Meanwhile in the main program it comes out to:

count = 3; // [HTML, HEAD, BODY]
html = "<html><head></head><body>This is a new HTML document.</body></html>";

These are small discrepancies but that is largely due to the simple HTML used. The one that causes the crash has rather significant differences.

I am absolutely stumped as to how the result can be so vastly different.

The documentation for HtmlDocument.Write(string) states that:

It is recommended that you write an entire valid HTML document using the Write method, including HTML and BODY tags. However, if you write just HTML elements, the Document Object Model (DOM) will supply these elements for you.

But I have no idea how the DomDocument is provided nor why they would be different in the first place. Both solutions are running in x64 Debug mode and Net-Framework 4.6.2. Both load the module: C:\WINDOWS\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll

How is it possible that these produce different results?! Any and all help welcome. Thanks in advance.

c#
winforms
webbrowser-control
asked on Stack Overflow Apr 28, 2021 by Benjamin Rauch

1 Answer

0

The difference in behavior stems from the registry entry as proposed by Jimi and linked here

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

To reiterate, in the above registry the main program had an entry "ApplicationFileName.exe"=dword:00002af9 while my new test-application didn't.

This, in and for itself, does not explain the crashing of mshtml.dll itself but since the question was about the difference in behavior I'll post this as the answer. The crash is most likely linked to the outdated version used by Visual Studio but I haven't yet had the chance to look into some of the proposed fixes for that.

answered on Stack Overflow May 3, 2021 by Benjamin Rauch

User contributions licensed under CC BY-SA 3.0