ATL/COM – Checking to see if Contained within Web Browser

There are two possible ways to create an ActiveX within HTML.  The first is via an Object HTML tag:


<object name="crypt2" width=0 height=0
classid="clsid:3352B5B9-82E8-4FFD-9EB1-1A3E60056904"
standby="Loading Chilkat Crypt2..."
type="application/x-oleobject"
codebase="http://www.chilkatsoft.com/download/ChilkatCrypt2.cab">
</object>

The 2nd way is to dynamically instantiate it within Javascript:

var crypt; 
crypt = new ActiveXObject("Chilkat.Crypt2");

If the object is created from within Javascript (via CreateObject), then the ActiveX has no way of knowing whether it is contained within an HTML page. If the object is created via an OBJECT tag, then it should be possible for the ActiveX to know that it exists within HTML, however, this does not seem to be the case anymore. Chilkat believes that this may have been possible with earlier versions of Internet Explorer, but it just doesn’t seem to work anymore.

Here is the ATL/COM code (internal to the ActiveX) that is used to check to see whether the object is contained within HTML:


bool CChilkatSomething::CheckWithinBrowser(void)
{
if (!m_spClientSite)
{
return false;
}

IServiceProvider *iSp = 0;
HRESULT hr = m_spClientSite->QueryInterface(IID_IServiceProvider,(void **)&iSp);
if ((hr == S_OK) && iSp)
{
IWebBrowser2 *iApp = 0;
iSp->QueryService(IID_IWebBrowserApp,IID_IWebBrowser2,(void **) &iApp);
if (iApp)
{
iApp->Release();
iSp->Release();
return true;
}
iSp->Release();
}

return false;
}

With the latest version of IE, it seems that m_spClientSite is always NULL, so it is impossible for the ActiveX to know that it is running client-side within a WebBrowser.

Tags :