none
MSHTML Interoperation works in Debug configuration, but fails in Release RRS feed

  • Question

  • Hi,
    I am writting web automation tool, it uses MSHTML to parse HTML document.
    My code is large and complex, but the main idea follows:

    string webpage = DownloadWebPage("http://....");
    HTMLDocumentClass document = new HTMLDocumentClass();
    document.write(webpage);
    document.close();
    form = (IHTMLFormElement)document.getElementById("myform");
    IHTMLElement[] inputs = form.Cast<IHTMLElement>().ToArray();
    I expect that this code will return all form inputs (expect where type is image) in array. It works OK in Debug configuration, however, when I build it as Release and run without debugging, the form.Cast<IHTMLElement>().ToArray() results in E_ACCESSDENIED error on 90% of real word sites (however, it works OK on like 10% of sites). When I run project again as Debug for the same sites where Release resulted in E_ACCESSDENIED, it works OK.
    What is possible cause of this bug? Is there any way to bypass it? Possibly I have to change something in my build configuration?

    Friday, December 25, 2009 10:14 AM

Answers

  • I solved this problem in following way: I removed new HTMLDocumentClass() code and replaced it with following one:

    WebBrowser htmlDocumentHoster = new WebBrowser();
    htmlDocumentHoster.DocumentText = "<html><head></head><body></body></html>";
    HTMLDocumentClass document = (HTMLDocumentClass)htmlDocumentHoster.Document.DomDocument;
    Actually I made in such a way that HTMLDocumentClass is created not by my code with direct constructor call. Instead it is created by System.Windows.Forms. Now everything works OK. It is definitelly MSHTML bug, because I never saw in documentation any idea with E_ACCESSDENIED is shown in current context.
    • Marked as answer by VASoftOnline Sunday, December 27, 2009 8:30 AM
    Sunday, December 27, 2009 8:30 AM

All replies

  • I would try to figure out if it is the API problem or .NET COM interop issue - write this code in native C++ (or in JScript) without CLR involved and run it.

    If it fails only in Release mode, debug the release application (e.g. use attach debugger in VS).

    If the cast throws E_ACCESSDENIED, try to rewrite it to something else to figure out what exactly is wrong. Or try to inspect the 'form' object before the casting.

    -Karel

    Friday, December 25, 2009 11:53 PM
    Moderator
  • I solved this problem in following way: I removed new HTMLDocumentClass() code and replaced it with following one:

    WebBrowser htmlDocumentHoster = new WebBrowser();
    htmlDocumentHoster.DocumentText = "<html><head></head><body></body></html>";
    HTMLDocumentClass document = (HTMLDocumentClass)htmlDocumentHoster.Document.DomDocument;
    Actually I made in such a way that HTMLDocumentClass is created not by my code with direct constructor call. Instead it is created by System.Windows.Forms. Now everything works OK. It is definitelly MSHTML bug, because I never saw in documentation any idea with E_ACCESSDENIED is shown in current context.
    • Marked as answer by VASoftOnline Sunday, December 27, 2009 8:30 AM
    Sunday, December 27, 2009 8:30 AM