Microsoft KB Archive/257717

= How To Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control =

Article ID: 257717

Article Last Modified on 5/11/2006

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q257717



SUMMARY
The Microsoft Knowledge Base article

172763 INFO: Accessing the Object Model from Within an ActiveX Control

explains how to obtain the IWebBrowser2 reference for the host window of an ActiveX control. However, often what developers actually want is a reference to the topmost IWebBrowser2, the one containing the frameset itself. This can be useful if you want to call the statusText command, for example, to set the value of the window status box before the page has been loaded. Because this property does not function on the WebBrowser control, calling it from the IWebBrowser2 of the embedded frame results in an error.



MORE INFORMATION
To retrieve the top-level IWebBrowser2 reference, get IServiceProvider from the client site and perform a QueryService for IID_IServiceProvider under the service SID_STopLevelBrowser (defined in Shlguid.h). From this second IServiceProvider, perform a QueryService for IID_IWebBrowser2 in the SID_SWebBrowserApp service.

The best place to perform this work is in the SetClientSite method of IOleObject:
 * 1) include 

if (ptr != NULL) {\ ptr->Release;\ ptr = NULL;\ }
 * 1) define COMRELEASE(ptr)\

IWebBrowser2 *browser = NULL;

STDMETHODIMP SetClientSite(IOleClientSite *pClientSite) {   HRESULT hr = S_OK; IServiceProvider *isp, *isp2 = NULL; if (!pClientSite) {       COMRELEASE(browser); }     else {       hr = pClientSite->QueryInterface(IID_IServiceProvider, reinterpret_cast(&isp)); if (FAILED(hr)) {           hr = S_OK; goto cleanup; }       hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast(&isp2)); if (FAILED(hr)) {           hr = S_OK; goto cleanup; }       hr = isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, reinterpret_cast(&browser)); if (FAILED(hr)) {           hr = S_OK; goto cleanup; }   cleanup: // Free resources. COMRELEASE(isp); COMRELEASE(isp2); return hr; } }

