1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 | """
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:
Filters out Explorer sinces we don't expect it to have HTML content or cookies. Explorer and IExplore are part of the Shell and therefore IShellWindows.
The mshmtl.py file must be fixed manually by inserting this at the top, otherwise you get NameErrors:
LONG_PTR = c_long
UINT_PTR = c_uint
wireHGLOBAL = wireHWND = wireHDC = wireHBITMAP = c_int
SAFEARRAY = POINTER # I"m quite sure this is wrong
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 ekoome@yahoo.com
"""
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, InternetExplorer, IWebBrowser2 #generated from SHDOCVW.DLL
from mshtml import IHTMLDocument2, IHTMLElement # generated using MSHTML.TLB
def IsInternetExplorer(name):
import re
from types import NoneType
if type(re.search(r'iexplore.exe$',name)) != NoneType:
return True
else:
return False
# Lets create an instance of ShellWindows 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 the full path of the EXE (IE or explorer)
if browser != None:
Name = BSTR()
hret = browser._get_FullName(byref(Name))
#Lets Check whether we are dealing with IE
if IsInternetExplorer(Name.value):
#Get browsing URL string
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
|