[ctypes-users] Connecting to running instances of IE sample
Brought to you by:
theller
From: Edwin K. <ek...@ya...> - 2004-08-26 15:27:47
|
I have included code to connect to running instances of IE on your computer. Enjoy -----------------Begin Code---------------------------- """ This sample connects to the running instances of IE on your computer and prints out the URL, Cookie- if any, and the HTML content of the site. You need to generate stub files for IE and MSHTML using the readtlb tool of ctypes. python ctypes\com\tools\readtlb.py c:\winnt\system32\MSHTML.TLB > mshtml.py python ctypes\com\tools\readtlb.py C:\windows\system32\SHDOCVW.DLL > ie6.py Known Issues: Won't work if Explorer is running. Explorer and IExplore are part of the Shell and therefore IShellWindows usage: python IEconnect.py You need the good ctypes module from http://starship.python.net/crew/theller/ctypes/ Sample based on information from http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q176/7/92.ASP&NoWebContent=1 By Eric Koome email ek...@ya... """ from ctypes import * from ctypes.com import GUID, HRESULT, IUnknown, CreateInstance from ctypes.com.automation import IDispatch, VARIANT, BSTR, oleaut32 from ctypes.wintypes import DWORD from win32con import NULL from ie6 import ShellWindows, IShellWindows, IWebBrowser2 #generated from SHDOCVW.DLL from mshtml import IHTMLDocument2, IHTMLElement # generated using MSHTML.TLB # Lets create an instance of ShellWiindows Interface pItf = CreateInstance(ShellWindows) windows = pItf #Get num of IE window by using IShellwindows nCount = c_long() hret = windows._get_Count(byref(nCount)) print "nCount", nCount for i in range(nCount.value): #Get IDispatch interfaces from IshellWindows disp = POINTER(IDispatch)() va = VARIANT(i) from ctypes.com.automation import VT_I4 # apparently IE doesn't like VT_INT (which automation uses), # it has to be VT_I4 oleaut32.VariantChangeType(byref(va), byref(va), 0, VT_I4) hret = windows.Item(va, byref(disp)) oleaut32.VariantClear(byref(va)) print "disp",disp #Get IwebBrowser2 Interfaces from IDispatch browser = POINTER(IWebBrowser2)() if disp != NULL: hret = disp.QueryInterface(byref(IWebBrowser2._iid_), byref(browser)) print "browser", browser #Get browsing URL string if browser != None: url= BSTR() hret = browser._get_LocationURL(byref(url)) print "url", url.value #Get IHTMLDocument2 from IWebBrowser2 htmlDisp = POINTER(IDispatch)() hret = browser._get_Document(byref(htmlDisp)) print "htmlDisp", htmlDisp doc = POINTER(IHTMLDocument2)() if htmlDisp != NULL: try: hret = htmlDisp.QueryInterface(byref(IHTMLDocument2._iid_), byref(doc)) print "doc", doc except: pass #Call get_cookie method of IHTMLDocument2 if doc != NULL: cookie = BSTR() try: hret = doc._get_cookie(byref(cookie)) print "cookie", cookie except: pass #Get IHTMLElement from IHTMLDocument2 element = POINTER(IHTMLElement)() hret = doc._get_body(byref(element)) print "element", element #Call get_outerHTML of IHTMLElement if element != NULL: html = BSTR() try: hret = element._get_outerHTML(byref(html)) print "html",html except: pass ------------------End Code----------------------------- Something for the people! Eric __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |