Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

Spying Internet Explorer: Receiving Events and Focus Information

I'm trying to spy a running Internet Explorer 6. I need to know which control has the focus, and on which control the mouse is hovering.

I found a solution where I open my own IE control and plug into an event source, but I need to find a solution for an already running IE. The main problem is, ::WindowFromPoint() doesn't make a distinction between controls (text boxes, combo boxes etc) insode Internet Explorer.

Can I instantiate a DOM object with the running IE instance, to ask the DOM object ? Or is it possible to register for events from the MSHTML control inside the existing and running IE ?

I have Visual C++ 6.0

1 Solution
Hi Alex,

there are some ways to connect to a running IE. You can search for the IE Window with FindWindowEx:

HWND ieWnd = FindWindowEx(0,0,L"Internet Explorer_Server",0);

Read http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindowex.asp for more information about FindWindowEx.

A more complex approach is to use the IShellWindows from shdocvw.dll:
create a ISellWindows object:

#import <shdocvw.dll>
SHDocVw::IShellWindowsPtr shell;

This object gives you access to all open Explorers. To get a IE Explorer:
You have a Enumeration interface to acces the registered items (explorers). Go through the items and pick out the IE Explorers:

SHDocVw::IWebBrowser2Ptr browser;
CComVariant var;
var.lVal = index;
IDispatchPtr pdisp = m_cpShell->Item(&var);
if ( pdisp )
      pdisp->QueryInterface(IID_IWebBrowser2, (void**)&pBrowser);      

There is also an example from Microsoft: http://support.microsoft.com/kb/176792

To get the IHTMDocument form the IWebBrowser, just call browser->GetDocument();
To get the HWND call browser->GetHWND();
or cast the browser object to a IOleWindow object and call oleWin->GetWindow(&wnd);

to get the IHTMLDocument form a HWND:
MSHTML::IHTMLDocument2Ptr getDocument(HWND hwnd)
    MSHTML::IHTMLDocument2Ptr tmp;
      LRESULT lRes = 0;      
      UINT nMsg = RegisterWindowMessage( L"WM_HTML_GETOBJECT" );
      LRESULT lRet = SendMessageTimeout( hwnd, nMsg,0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
      if(lRet == 0) {
            return 0;
          if (ObjectFromLresult(lRes,__uuidof(MSHTML::IHTMLDocument2),0,(void**)&tmp) == S_OK )
            return tmp;
      return 0;

As soon as you have the IHTMLDocument, you can connect to the events with the DIID_HTMLDocumentEvents2 interface.

You could also use the MSAA Hooks to track all focus and mouse events. When the hook calls your eventcallback, just check if the calling window is your Explorer window you want to spy. With this event you can get the IAccessible object of the current control in the IE Window. Use AccessibleObjectFromEvent to get the IAccessible.

PC-AlexAuthor Commented:
Thanks, chip3d for the help. It was not exactly what I needed (I already had the HWND of the MSHTML through ::WindowFromPoint()), but after reading your code and some further testing I found KB Q249232 (http://support.microsoft.com/kb/249232), and that was exactly what I needed.

So you gave me some very valuable hints to find the solution.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now