Spying Internet Explorer: Receiving Events and Focus Information

Posted on 2006-04-02
Last Modified: 2010-05-18
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

Question by:PC-Alex
    LVL 4

    Accepted 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 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:

    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.

    LVL 1

    Author Comment

    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 (, and that was exactly what I needed.

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now