Why is my implementation of IDocHostUIHandler ignored

I created a derived control from a WebBrowser that has its own implementation of IDocHostUIHandler , following the idea of CreateWebBrowserSiteBase :

The WebBrowser.WebBrowserSite class provides standard implementations of the OLE IDocHostUIHandler interface. You can provide your own implementation of this interface or the implementation of any other WebBrowser ActiveX control interface to configure control behavior.

A problem that does not work. My code is as follows:

 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] public MyBrowser(){} protected override WebBrowserSiteBase CreateWebBrowserSiteBase() { var manager = new NewWebBrowserSite(this); return manager; } protected class NewWebBrowserSite : WebBrowserSite, UnsafeNativeMethods.IDocHostUIHandler { private MyBrowser host; public NewWebBrowserSite(MyBrowser h) : base(h) { this.host = h; } int UnsafeNativeMethods.IDocHostUIHandler.ShowContextMenu(int dwID, NativeMethods.POINT pt, object pcmdtReserved, object pdispReserved) { MyBrowser wb = (MyBrowser)this.host; // other code } // rest of IDocHostUIHandler methods } 

My questions:

  • Do I need to implement other interfaces to make it work;
  • This is by design, I read post that it is related to a bug in the implementation of the .net framework. WebBrowser

I know ICustomDoc.SetUIHandler can go, but that is not what I am looking for.

I had the idea at some point to abandon C # and do it with unmanaged code. This is true?

+3
c # browser webbrowser-control
source share
2 answers

You cannot just override the interfaces implemented by the class. If the methods for IDocHostUIHandler are not marked as virtual, you cannot replace them.

The fact that an interface is defined in UnsafeNativeMethods is also the key that you probably shouldn't bother with it unless you have a very good idea of ​​what you are doing.

+1
source share

I just solved the exact same problem: how to provide a custom implementation of IDocHostUIHandler control for WinForms WebBrowser . The problem is that the WebBrowserSite base class has already implemented its own version of IDocHostUIHandler (which is the internal interface, so it is impossible to explicitly repeat the implementation in the NewWebBrowserSite derived class). However, theoretically, there should not be a problem with the implementation of another C # interface with the same GIID layout and methods (since in this particular case this applies to the entire COM client - the basic ActiveX WebBrowser control).

Unfortunately, this was not possible until .NET 4.0. Fortunately, this is now using the new ICustomQueryInterface function:

 protected class NewWebBrowserSite : WebBrowserSite, UnsafeNativeMethods.IDocHostUIHandler ICustomQueryInterface { private MyBrowser host; public NewWebBrowserSite(MyBrowser h): base(h) { this.host = h; } int UnsafeNativeMethods.IDocHostUIHandler.ShowContextMenu(int dwID, NativeMethods.POINT pt, object pcmdtReserved, object pdispReserved) { MyBrowser wb = (MyBrowser)this.host; // other code } // rest of IDocHostUIHandler methods // ICustomQueryInterface public CustomQueryInterfaceResult GetInterface(ref Guid iid, out IntPtr ppv) { if (iid == typeof(UnsafeNativeMethods.IDocHostUIHandler).GUID) { ppv = Marshal.GetComInterfaceForObject(this, typeof(UnsafeNativeMethods.IDocHostUIHandler), CustomQueryInterfaceMode.Ignore); } else { ppv = IntPtr.Zero; return CustomQueryInterfaceResult.NotHandled; } return CustomQueryInterfaceResult.Handled; } } 
+4
source share

All Articles