Link to home
Start Free TrialLog in
Avatar of prangel
prangel

asked on

How to Monitor Events in Explorer

Hi All!

I find that I can create an instance of Windows Explorer and monitor its' internet traffic events like this:

  HRESULT hr = CoCreateInstance( CLSID_ShellBrowserWindow, // Creates an Instance of Windows Explorer.
                                 NULL,
                                 CLSCTX_LOCAL_SERVER,
                                 IID_IWebBrowser2,
                                 (void**)&m_pIE);
 
  m_pIE->put_Visible(VARIANT_TRUE);

  BOOL bAdvised = AfxConnectionAdvise( m_pIE,
                                       DIID_DWebBrowserEvents2,
                                       m_pIEEvents->GetInterface(&IID_IUnknown),
                                       TRUE,
                                       &m_dwCookie);


Great.  But what I want to do is to monitor internet events on an instance of Explorer that is already running.  So I did manage to get this to work:

  CString msg;
  HRESULT hresult;
  VARIANT arg;
  arg.vt = VT_ERROR;
  arg.scode = DISP_E_PARAMNOTFOUND;

  IUnknown *punk = (IUnknown *)NULL;

  CLSID clsid;                                

  CLSIDFromProgID( L"InternetExplorer.Application.1",
                  &clsid);

  hresult = GetActiveObject( clsid,
                            NULL,
                            &punk);

  if(!SUCCEEDED(hresult)){

   AfxMessageBox("Not FOUND", MB_OK);

   return TRUE;

  }

  hresult = punk->QueryInterface( IID_IWebBrowser2,
                                 (LPVOID *)&m_pIE);

  if(!SUCCEEDED(hresult)){
      AfxMessageBox("QueryInterface() failed", MB_OK);
      punk->Release();
      return TRUE;
  }

  punk->Release();

  // Set up the event sink
  BOOL bAdvised = AfxConnectionAdvise( m_pIE,
                                      DIID_DWebBrowserEvents2,
                                      m_pIEEvents->GetInterface(&IID_IUnknown),
                                      TRUE,
                                      &m_dwCookie);

Fine.  That allows me to monitor a previously running version of IE.  But, I haven't been able to find out what the ProgID is for Explorer.exe.  Man do I feel dense.  My brain hurts.  This can't be that hard.  Will sombody please put me out of my misery?

Thank, Larry
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi prangel,

with 'ProgID' you mean the 'InternetExplorer.Application.1'
from your code?

If so, why not simply use i.e.

hresult = GetActiveObject( CLSID_InternetExplorer,          NULL,&punk);

hope that helps,

ZOPPO
Avatar of prangel
prangel

ASKER

Hey ZOPPO! Thanks for looking at my little problem.

Yea, I knew that GetActiveObject(CLSID_InternetExplorer . . .  ) works fine.  And when I use CoCreateInstance(CLSID_ShellBrowserWindow . . .  ) to create and control an instance of Windows Explorer (not IE) that works fine too.  But when I try to hook up to an already-runnung instance of Windows Explorer with GetActiveObject(CLSID_ShellBrowserWindow . . .  ), I get back error #-2147221021, 'Operation Unavailable'.  That is what I am trying to do.  I need to do an AfxConnectionAdvise() to a previously running instance of Windows Explorer (not IE) to monitor internet traffic that originates from Windows Explorer.

Thanks for your help,

Larry
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prangel

ASKER

ZOPPO!

I think that I got a handle on it.  

(By the way AfxOleInit() was in there.)  

As a result of some testing I was doing to figure out why it worked for you and didn't for me, I discovered that an instance of Explorer.exe (Not IE) would respond properly to GetActiveObject(CLSID_InternetExplorer . . .  ).  Weird, but I guess that it is the IE control, contained be Explorer, that is answering the 'phone'.  Not Explorer itself.  So, the key accessing the internet functionaliy in Explorer is to use IE's CLSID, not Explorer's, with GetActiveObject().

I appriciate your help for helping me get on the right track and I am going to award you the points.

Thanks!

Larry
hm, ok, thanks a lot ... sorry that you had to solve the problem yourself :o)

have a nice day,

regards,

ZOPPO