Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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:
#include <SHLGUID.h>

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

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<void **>(&isp));
		if (FAILED(hr)) 
		{
			hr = S_OK;
			goto cleanup;
		}
		hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast<void **>(&isp2));
		if (FAILED(hr))
		{
			hr = S_OK;
			goto cleanup;
		}
		hr = isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, reinterpret_cast<void **>(&browser));
		if (FAILED(hr)) 
		{
			hr = S_OK;
			goto cleanup;
		}
	cleanup:
		// Free resources.
		COMRELEASE(isp);
		COMRELEASE(isp2);
		return hr;
	}
}
				

↑ Back to the top


References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
172763 INFO: Accessing the Object Model from Within an ActiveX Control
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB257717, kbhowto, kbctrl

↑ Back to the top

Article Info
Article ID : 257717
Revision : 3
Created on : 5/11/2006
Published on : 5/11/2006
Exists online : False
Views : 473