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

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

  • Tuesday, May 19, 2009 10:08 PM
     
     
    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?


    Thanks,

    Jeff


All Replies

  • Tuesday, May 19, 2009 10:16 PM
     
     Answered
    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 jeffdavMVP Tuesday, May 19, 2009 10:20 PM
    • Marked As Answer by Jeff99999 Friday, May 22, 2009 5:23 AM
    •  
  • Friday, May 22, 2009 5:23 AM
     
     
    Thanks jeffdav,  that was exactly what I was looking for.

    Jeff
  • Friday, September 17, 2010 3:35 AM
     
     

    If I want to get IWebBrowser first from HWND,

    How to do it?

    Thanks for your attention!

  • Friday, September 17, 2010 2:51 PM
     
     
    You can't.  
  • Sunday, September 19, 2010 5:11 AM
     
     

    resort to Jeff:

    How to find current register shellwindow by using FindWindowSW?

    http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/ce5bf873-6e35-492f-aeae-449225f6ac34

  • Monday, September 20, 2010 6:25 PM
     
     

    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?

  • Tuesday, September 21, 2010 1:17 AM
     
     
    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 3:47 PM
     
     
    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.