1

I'm not the greatest with COM objects but, I have a need to extend the WebBrowser control to support flags in the navigate method (Specifically to prevent reading/writing from cache).

From what I gather I'll need to implement IWebBrowser2 to some extent. Can I just implement the Navigate method or do I need to define all methods in the interface?

I've found some examples for attaching/detaching an event sink to extend the events of the web browser but very little around the actual Methods.

Can I use the underling ActiveXInstance of the WebBrowser control? If I create a class that implements IWebBrowser2::Navigate, and cast a variable to it that class, assigning the WebBrowser control ActiveXInstance then attempt to Navigate I get a COM exception HRESULT E_FAIL

I found this but not sure if the underlying control is still ShDocVw as I didn't see it in my COM objects (Target FW .Net 3.5): Web Browser to handle pop ups within the application

internal ShDocVw.WebBrowser ActiveXWebBrowser { get; private set; }`

new public void Navigate(string url)
{
    this.Navigate(url, axNativeMethods.WebBrowserNavigateFlags.NoReadFromCache | axNativeMethods.WebBrowserNavigateFlags.NoWriteToCache, string.Empty, new byte[] { }, string.Empty);
}

public void Navigate(string url, axNativeMethods.WebBrowserNavigateFlags flags, string targetFrameName, byte[] postData, string headers)
{

    this.ActiveXWebBrowser = (ShDocVw.WebBrowser)this.ActiveXInstance;`

    object flagsObj = (flags == axNativeMethods.WebBrowserNavigateFlags.None) ? null : (object)flags;
    object targetFrameNameObj = targetFrameName;
    object headersObj = headers;
    object postDataObj = postData;

    ActiveXWebBrowser.Navigate(url, ref flagsObj, ref targetFrameNameObj, ref postDataObj, ref headersObj);
}
Community
  • 1
  • 1
Sloanstarr
  • 11
  • 3
  • No. Nothing you can't fix with a simple variable in your class. Set it before you call Navigate, read it back in your DocumentCompleted event handler. – Hans Passant Aug 17 '15 at 09:01
  • I fixed it with some changes, I'm not sure of the rules, should i change my question or post my answer as a separate response? Basically, I had to change the casting, I was using IWebBrowser2, but I needed to cast it as ShDocVw.WebBrowser - Once that was done, everything worked fine. I like the extension methods idea, and I'm going to explore that some more, but since I already had a class that inherited from WebBrowser, I don't think it wasn't quite the right fit here. – Sloanstarr Aug 19 '15 at 00:53

1 Answers1

0

This is a perfect use case for extension methods. Basically, they allow you to define methods in a static class that appear to be bound directly to the type being extended, and which behave much like public instance methods on the type, except that only public or internal (in the case of friend assemblies or the extension class and extended class being in the same assembly) are available.

Here's an example snippet:

public static class WebBrowserExtensions
{
    public static void Navigate(this IWebBrowser2 browser, string url)
    {
        browser.Navigate(url, /* fill in arguments as necessary*/)
    }
}