Extend a webbrowser control using ICustomQueryInterface

Begin ChatGPT generated response

To extend a WebBrowser control using ICustomQueryInterface, you can create a new class that implements the ICustomQueryInterface interface and then use the SetSite method of the WebBrowser control to set the new class as the site for the control. Here are the steps you can follow:

  1. Create a new class that implements the ICustomQueryInterface interface. The ICustomQueryInterface interface provides a way to intercept queries for interfaces that are not normally exposed by an object. Here’s an example implementation:

class MyCustomQueryInterface : public ICustomQueryInterface { public: // ICustomQueryInterface methods HRESULT STDMETHODCALLTYPE GetInterface(const IID& iid, void** ppv); };

  1. In your WPF window or user control, create a new instance of the WebBrowser control and set its site to an instance of your new class that implements ICustomQueryInterface:

MyCustomQueryInterface* pCustomQueryInterface = new MyCustomQueryInterface(); IWebBrowser2* pWebBrowser2 = m_pWebBrowser->Document; pWebBrowser2->SetSite(pCustomQueryInterface);

  1. Implement the GetInterface method of your ICustomQueryInterface class to intercept queries for interfaces that are not normally exposed by the WebBrowser control. For example, you can intercept queries for the IOleCommandTarget interface to customize the behavior of the control:

HRESULT STDMETHODCALLTYPE MyCustomQueryInterface::GetInterface(const IID& iid, void** ppv) { if (iid == IID_IOleCommandTarget) { // return a pointer to your custom IOleCommandTarget implementation here return S_OK; } return E_NOINTERFACE; }

That’s it! You can now use your customized WebBrowser control in your WPF application with the behavior that you defined in your implementation of the ICustomQueryInterface interface. Note that the specific interfaces that you intercept and the behavior that you define will depend on your specific requirements.

End ChatGPT generated response


Wrong language aside (ICustomQueryInterface is , this response is surprisingly close. The AI correctly recognizes that the way to extend a webbrowser control is is via SiteSite, however it attributes the method to IWebBrowser2 instead of IOleObject.  And it implements IOleCommandTarget  for no apparent reason. The way to change a webbrowser control’s behavior via IOleCommandTarget  is limited to handling of menu and toolbar commands and script/alert error message boxes. Generally speaking you want to turn on the FEATURE_BROWSER_EMULATION Feature control and implement IDocHostUIHandler/IServiceProvider.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.