3

I am hosting a web browser control, initialized with IWebBrowser2.put_Silent(VARIANT_TRUE).

However, when calling IWebBrowser2.Navigatewith a malformed path, I still get a message box to the equivalent of

"file:///D://htmlstuff/page.html" was not found. Make sure the path or internet address is correct.

The issue here is the accidental double-slash specified by the user (D:\htmlstuff instead of D:\htmlstuff).

I can catch this particular problem earlier, before doing the Navigate, but I'm concerned that this message box still occurs, as I cannot verify every possible URL (or other possible cause that makes the webbrowser control ignore the Silent flag).

I would expect to get an error code returned silently.

Any ideas? Is there a "even more silent" option?

Windows 8.0, IE 10.0.9200.16750

peterchen
  • 40,917
  • 20
  • 104
  • 186

2 Answers2

2

This is a nasty behavior of the WebBrowser control, which I experience with VBA and UserForms. My workaround was to load the target URL into an iframe within a small hosting page. This way no alert pops up, and you can query if the target URL did indeed load or not.

Call the hosting page with <PathTo>/framehost.html#<TargetURL>

<!DOCTYPE html>
<head>
    <title>Frame Host</title>
    <script>

window.onload = function() {
    var frame = document.getElementById('MyFrame');
    frame.src = location.hash.substring(1);
    frame.onload = function() {
        try {
            var doc = frame.contentDocument;
            alert("Loaded " + frame.src);
        } catch(e) {
            alert("Failed to load " + frame.src)
        }
    }
}

    </script>
</head>
<body>
    <iframe id="MyFrame" src="about:blank"></iframe>
</body>
</html>
Wolfgang Kuehn
  • 12,206
  • 2
  • 33
  • 46
1

Try handling NavigateError event on the underlying WebBrowser ActiveX control. I have an example showing how handle the "underlying" WebBrowser events like that. It's for WPF, but it can be easily adapter for WinForms (using WebBrowser.ActiveXInstance).

Alternatively, I think you should be able to handle this kind of messages by implementing IDocHostShowUI::ShowMessage. I have another example showing how to implement IDocHostUIHandler on WebBrowser site object. The same approach can be used for IDocHostShowUI.

Disclaimer: I haven't verified either of these two potential solutions.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Thanks! - I tried NavigateError, but it doesn't trigger. IDocHostSHowUI seems to be harder to implement, as it needs to be implemented on the object that also implements IOleClientSite, according to msdn (http://msdn.microsoft.com/en-us/library/aa753269(v=vs.85).aspx) – peterchen Mar 06 '14 at 08:55
  • I haven't messed with it in a loooooooong while, but look at this: [WebBrowserEx - Extended WebBrowser Control](http://webbrowserex.codeplex.com/). – Paulo Morgado Mar 06 '14 at 15:48
  • 1
    @peterchen, you can subclass the base's `IOleClientSite`: http://stackoverflow.com/q/19717787/1768303 – noseratio Mar 08 '14 at 23:26
  • @Noseratio: C++/ATL unfortunately (I've fixed the misleading tags). https://groups.google.com/d/msg/microsoft.public.vc.atl/tPB41rx6XAs/W3tjg53EDpwJ is the closest I can find. Anyway, thanks! – peterchen Mar 10 '14 at 09:03
  • @peterchen, no problem. I have another [code snippet](http://stackoverflow.com/a/18334802/1768303) which may help doing this with ATL :) If `IDocHostSHowUI` does work, you can always resort to [CBTHook](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644977(v=vs.85).aspx). – noseratio Mar 10 '14 at 09:10