How does one get a IWebBrowser2 object from a hwnd (TabWindowClass)


  • I'm trying to write some code to manipulate and get information from the tabs in IE.  Among other things, I'm interested in finding out from each tab what URL it points to.  I know how to iterate through the windows in IE and find the HWNDs of type TabWindowClass.  What I'd like to know is:

    1) Now that I have a TabWindowClass HWND, what can I do with it?  (I wasn't able to find out much about TabWindowClass on MSDN)

    2) I've read that each tab has its own WebBrowser2 object.  How do I get a WebBrowser2 from a HWND?



    Tuesday, May 19, 2009 10:08 PM


All replies

  • Use the Accessibility hack described here:

    Once you have the IHTMLDocument, to get the IWebBrowser2:

    1. QueryInterface() the IHTMLDocument2 for IID_IServiceProvider.
    2. QueryService() the IServiceProvider for SID_SWebBrowserApp, IID_IWebBrowser2.

    For future reference, see:
    • Proposed as answer by jeffdav Tuesday, May 19, 2009 10:20 PM
    • Marked as answer by Jeff99999 Friday, May 22, 2009 5:23 AM
    Tuesday, May 19, 2009 10:16 PM
  • Thanks jeffdav,  that was exactly what I was looking for.

    Friday, May 22, 2009 5:23 AM
  • If I want to get IWebBrowser first from HWND,

    How to do it?

    Thanks for your attention!

    Friday, September 17, 2010 3:35 AM
  • You can't.  
    Friday, September 17, 2010 2:51 PM
  • resort to Jeff:

    How to find current register shellwindow by using FindWindowSW?

    Sunday, September 19, 2010 5:11 AM
  • FindWindowSW() returns an HWND and an Automation pointer; it doesn't take an HWND as input and provide an IWebBrowser pointer as output, which is the request that was made.  

    Though I think you're implying there is a way to do it with IShellWindows in general, and I guess you could enumerate all of them and interrogate the automation pointer you get back to get the HWND and IWebBrowser and compare with the HWND you're looking for... assuming IShellWindows still works with Low-Rights IE (which I thought it did not).  Maybe that has been fixed?

    Monday, September 20, 2010 6:25 PM
  • Thanks jeff, your enumeration idea is in accord with my current work. but I believe it's inefficient compared with getting the current register shellwindow's HWND directly for using FindWindowSW(). disappointly, I don't know how to use the param of lCookie passed by DShellWindowsEvents.WindowRegistered.
    Tuesday, September 21, 2010 1:17 AM
  • Yeah, I'm not sure either.  I seem to recall that they wanted to deprecate the whole ShellWindows thing but had to document it for legal reasons.  It's really not meant for public consumption.  It'd be nice if they created a cleaned-up alternative.
    Tuesday, September 21, 2010 3:47 PM