IOleCommandTarget, CGID_MSHTML and ActiveX controls
If you write an application that hosts the WebBrowser Control, and you want the control to do something, you can send commands to mshtml via the IOleCommandTarget interface.
However, if you are an ActiveX control and you want to send CGID_MSHTML commands, you may try something like this:
...
IOleCommandTarget *pCommandTarget = NULL;
hr = _punkSite->QueryInterface(IID_IOleCommandTarget, (void **)&pCommandTarget);
if (SUCCEEDED(hr))
{
hr = pCommandTarget->Exec(&CGID_MSHTML, IDM_FOO, 0, NULL, NULL);
pCommandTarget->Release();
}
...
If you have tried this, you will see that it fails for CGID_MSHTML commands. It fails because your control is hosted by an object inside of mshtml.dll that delegates all incoming commands to the nearest DocHostUIHandler. For IE, that is implemented by an object in shdocvw.dll (or ieframe.dll for IE7+). That object does not recognize CGID_MSHTML commands.
In order for your call to be routed correctly, you need to get an object "above" the object that is hosting your control. To do this, you can first ask the client site for an IServiceProvider:
...
IServiceProvider *pServiceProvider = NULL;
hr = _punkSite->QueryInterface(IID_IServiceProvider, (void **)&pServiceProvider);
if (SUCCEEDED(hr))
{
IOleCommandTarget *pCommandTarget = NULL;
hr = pServiceProvider->QueryService(SID_SContainerDispatch, IID_IOleCommandTarget, &pCommandTarget);
if (SUCCEEDED(hr))
{
hr = pCommandTarget->Exec(&CGID_MSHTML, IDM_FOO, 0, NULL, NULL);
pCommandTarget->Release();
}
pServiceProvider->Release();
}
By asking for the ContainerDispatch's command target, you get the correct target for MSHTML commands.
Comments
Anonymous
January 11, 2007
PingBack from http://www.ie7security.net/2007/01/11/iolecommandtarget-cgid_mshtml-and-activex-controls/Anonymous
January 21, 2007
PingBack from http://www.ie7security.net/2007/01/21/iolecommandtarget-cgid_mshtml-and-activex-controls-3/Anonymous
February 08, 2007
PingBack from http://www.ie7security.net/2007/02/08/iolecommandtarget-cgid_mshtml-and-activex-controls-2/Anonymous
August 14, 2008
PingBack from http://www.ie7security.net/2008/08/14/iolecommandtarget-cgid_mshtml-and-activex-controls-3/Anonymous
February 21, 2009
PingBack from http://www.ie7security.net/2009/02/21/iolecommandtarget-cgid_mshtml-and-activex-controls-4/Anonymous
May 03, 2009
PingBack from http://www.ie7security.net/2009/05/03/iolecommandtarget-cgid_mshtml-and-activex-controls-5/