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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.