Saturday, June 21, 2008

the internet explorer object cookbook

When you start writing Internet Explorer extensions, or dealing with the DOM and the Webbrowser control at all, you frequently find you have an object of type X and you need to get the corresponding object of type Y. There is a lot of myth and bad advice on the web surrounding how to do some of these things.

The "site" in all of the following refer to the IUnknown you are given by Internet Explorer when it calls your IObjectWithSite::SetSite() method.

IWebBrowser2 from site

1. QueryInterface() your site for IID_IServiceProvider.
2. QueryService() the IServiceProvider for SID_STopLevelBrowser, IID_IServiceProvider.
3. QueryService() the top level IServiceProvider for SID_SWebBrowserApp, IID_IWebBrowser2.

IHTMLDocument2 from IWebBrowser2

1. Call IWebBrowser2::get_Document().
2. QueryInterface() the resulting IDispatch pointer for IID_IHTMLDocument2.

IHTMLDocument2 from site

1. Follow steps for "IWebBrowser2 from site" above.
2. Follow steps for "IHTMLDocument2 from IWebBrowser2" above.

IWebBrowser2 from IHTMLDocument2

1. QueryInterface() the IHTMLDocument2 for IID_IServiceProvider.
2. QueryService() the IServiceProvider for SID_SWebBrowserApp, IID_IWebBrowser2.

IHTMLDocument2 from IHTMLElement

1. Call IHTMLElement::get_Document();
2. QueryInterface() the resulting IDispatch pointer for IID_IHTMLDocument2.

IHTMLDocument2 from HWND

1. Use the accessibility hack described at here.

No comments: