<p>I wrote an Active X plugin for IE7 which implements IObjectWithSite besides some other necessary interfaces (note no IOleClient). This interface is queried and called by IE7. During the SetSite() call I retrieve a pointer to IE7's site interface which I can use to retrieve the IHTMLDocument2 interface using the following approach:</p>

<pre><code>IUnknown *site = pUnkSite; /* retrieved from IE7 during SetSite() call */
IServiceProvider *sp = NULL;
IHTMLWindow2 *win = NULL;
IHTMLDocument2 *doc = NULL;

if(site) {
    site-&gt;QueryInterface(IID_IServiceProvider, (void **)&amp;sp);
    if(sp) {
        sp-&gt;QueryService(IID_IHTMLWindow2, IID_IHTMLWindow2, (void **)&amp;win);
        if(win) {
            win-&gt;get_document(&amp;doc);
        }
    }
}
if(doc) {
    /* found */
}
</code></pre>

<p>I tried a similiar approach on PIE as well using the following code, however, even the IPIEHTMLWindow2 interface cannot be acquired, so I'm stuck:</p>

<pre><code>IUnknown *site = pUnkSite; /* retrieved from PIE during SetSite() call */
IPIEHTMLWindow2 *win = NULL;
IPIEHTMLDocument1 *tmp = NULL;
IPIEHTMLDocument2 *doc = NULL;

if(site) {
    site-&gt;QueryInterface(__uuidof(*win), (void **)&amp;win);
    if(win) { /* never the case */
        win-&gt;get_document(&amp;tmp);
        if(tmp) {
            tmp-&gt;QueryInterface(__uuidof(*doc), (void **)&amp;doc);
        }
    }
}
if(doc) {
    /* found */
}
</code></pre>

<p>Using the IServiceProvider interface doesn't work either, so I already tested this.</p>

<p>Any ideas?</p>

[{'answer_id': 63430, 'author': 'gnobal', 'author_id': 7748, 'author_profile': 'https://Stackoverflow.com/users/7748', 'pm_score': 3, 'selected': True, 'text': '<p>I found the following code in the Google Gears code, <a href="http://code.google.com/p/gears/source/browse/trunk/gears/base/ie/activex_utils.cc?r=2157" rel="nofollow noreferrer">here</a>. I copied the functions I think you need to here. The one you need is at the bottom (GetHtmlWindow2), but the other two are needed as well. Hopefully I didn\'t miss anything, but if I did the stuff you need is probably at the link.</p>\n\n<pre><code>#ifdef WINCE\n// We can\'t get IWebBrowser2 for WinCE.\n#else\nHRESULT ActiveXUtils::GetWebBrowser2(IUnknown *site, IWebBrowser2 **browser2) {\n  CComQIPtr&lt;IServiceProvider&gt; service_provider = site;\n  if (!service_provider) { return E_FAIL; }\n\n  return service_provider-&gt;QueryService(SID_SWebBrowserApp,\n                                        IID_IWebBrowser2,\n                                        reinterpret_cast&lt;void**&gt;(browser2));\n}\n#endif\n\n\nHRESULT ActiveXUtils::GetHtmlDocument2(IUnknown *site,\n                                       IHTMLDocument2 **document2) {\n  HRESULT hr;\n\n#ifdef WINCE\n  // Follow path Window2 -&gt; Window -&gt; Document -&gt; Document2\n  CComPtr&lt;IPIEHTMLWindow2&gt; window2;\n  hr = GetHtmlWindow2(site, &amp;window2);\n  if (FAILED(hr) || !window2) { return false; }\n  CComQIPtr&lt;IPIEHTMLWindow&gt; window = window2;\n  CComPtr&lt;IHTMLDocument&gt; document;\n  hr = window-&gt;get_document(&amp;document);\n  if (FAILED(hr) || !document) { return E_FAIL; }\n  return document-&gt;QueryInterface(__uuidof(*document2),\n                                  reinterpret_cast&lt;void**&gt;(document2));\n#else\n  CComPtr&lt;IWebBrowser2&gt; web_browser2;\n  hr = GetWebBrowser2(site, &amp;web_browser2);\n  if (FAILED(hr) || !web_browser2) { return E_FAIL; }\n\n  CComPtr&lt;IDispatch&gt; doc_dispatch;\n  hr = web_browser2-&gt;get_Document(&amp;doc_dispatch);\n  if (FAILED(hr) || !doc_dispatch) { return E_FAIL; }\n\n  return doc_dispatch-&gt;QueryInterface(document2);\n#endif\n}\n\n\nHRESULT ActiveXUtils::GetHtmlWindow2(IUnknown *site,\n#ifdef WINCE\n                                     IPIEHTMLWindow2 **window2) {\n  // site is javascript IDispatch pointer.\n  return site-&gt;QueryInterface(__uuidof(*window2),\n                              reinterpret_cast&lt;void**&gt;(window2));\n#else\n                                     IHTMLWindow2 **window2) {\n  CComPtr&lt;IHTMLDocument2&gt; html_document2;\n  // To hook an event on a page\'s window object, follow the path\n  // IWebBrowser2-&gt;document-&gt;parentWindow-&gt;IHTMLWindow2\n\n  HRESULT hr = GetHtmlDocument2(site, &amp;html_document2);\n  if (FAILED(hr) || !html_document2) { return E_FAIL; }\n\n  return html_document2-&gt;get_parentWindow(window2);\n#endif\n}\n</code></pre>\n'}, {'answer_id': 71281, 'author': 'anselm', 'author_id': 4735, 'author_profile': 'https://Stackoverflow.com/users/4735', 'pm_score': 2, 'selected': False, 'text': '<p>Well I was aware of the gears code already. The mechanism gears uses is based on a workaround through performing an explicit method call into the gears plugin from the gears loader to set the window object and use that as site interface instead of the IUnknown provided by IE Mobile in the SetSite call. Regarding to the gears code the Google engineers are aware of the same problem I\'m asking and came up with this workaround I described.</p>\n\n<p>However, I believe there must be another more "official" way of dealing with this issue since explicitely setting the site on an Active X control/plugin isn\'t very great. I\'m going to ask the MS IE Mobile team directly now and will keep you informed once I get a solution. It might be a bug in IE Mobile which is the most likely thing I can imagine of, but who knows...</p>\n\n<p>But thanks anyways for your response ;))</p>\n'}]
['https://Stackoverflow.com/questions/45658', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4735/']