[python-win32] How to get IWebBrowser2 from a HWND
salvatore ventura
venturasalvatore at yahoo.it
Fri Jul 11 03:42:03 CEST 2008
Hi,
I am trying to get the IWebBrowser2 object from the HWND of an open IE window.
I have seen this topic covered in many languages, and I am following the same steps, but in python, yet I am hitting a wall that I can't figure how to break... So, yes, I have been 'googling', browsing and testing many options, but without the desired result.
So, here details about my python installation on WinXP:
- python-2.5.2.msi
- pywin32-210.win32-py2.5.exe
- comtypes-0.4.2.win32.exe
- ServProv.tlb --> used to generate the IServiceProvider wrappers
Now, my scenario is IE7 already open on a webpage 'Page1', and a modal popup webpage 'Modal1', which is not a Messagebox/Alert. Once that is up, the following code is supposed to retrieve the IWebBrowser2 object of the 'Modal1' window.
(assume all the imports are done correctly)
# get the window, and then reach the 'Internet Explorer_Server'
win1 = win32gui.FindWindow('Internet Explorer_TridentDlgFrame', 'Modal1')
win1 = winGuiAuto.findControl(win1, None, 'Internet Explorer_Server', None)
# get ready to get the IHTMLDocument2 from win1
#
#
oleacc = oledll.LoadLibrary ( 'oleacc.dll' )
WM_HTML_GETOBJECT = windll.user32.RegisterWindowMessageA ('WM_HTML_GETOBJECT' )
lResult = c_ulong ( )
pIHTMLDocument2 = POINTER ( IHTMLDocument2 ) ( )
ret = windll.user32.SendMessageTimeoutA ( c_int ( win1 ), c_int (WM_HTML_GETOBJECT),c_int ( 0 ), c_int ( 0 ), c_int ( SMTO_ABORTIFHUNG ), c_int ( 1000 ), byref (lResult ) )
oleacc.ObjectFromLresult ( lResult, byref ( IHTMLDocument2._iid_ ), 0,byref (pIHTMLDocument2 ) )
# at this point pIHTMLDocument2 is at the right window, as the 'title' property gives me what I expect
# now followup to get the parent ...
ihtmlwin = pIHTMLDocument2.parentWindow
# ... the service provider ...
pIServiceProvider = ihtmlwin.QueryInterface(IServiceProvider, IServiceProvider._iid_)
# ... and the IWebBrowser2:
ie = pIServiceProvider.QueryService(IWebBrowserApp._iid_, IWebBrowser2._iid_)
# which - of course - fails.
Error returned:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
COMError: (-2147467262, 'No such interface supported', (None, None, None, 0, None))
which would imply that I am in the wrong 'place'. But I can't see how.
I must be doing something wrong, but can't figure what. Any help very much appreciated, and sorry for the long post.
regards,
.salvo
ps. this a link to one of the places that describe the procedure i have followed:
http://www.forum.it-berater.org/index.php?topic=574.msg1060#msg1060
More information about the python-win32
mailing list