-
Notifications
You must be signed in to change notification settings - Fork 784
/
iebutton.py
217 lines (175 loc) · 6.91 KB
/
iebutton.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: latin-1 -*-
# PyWin32 Internet Explorer Button
#
# written by Leonard Ritter (paniq@gmx.net)
# and Robert Förtsch (info@robert-foertsch.com)
"""
This sample implements a simple IE Button COM server
with access to the IWebBrowser2 interface.
To demonstrate:
* Execute this script to register the server.
* Open Pythonwin's Tools -> Trace Collector Debugging Tool, so you can
see the output of 'print' statements in this demo.
* Open a new IE instance. The toolbar should have a new "scissors" icon,
with tooltip text "IE Button" - this is our new button - click it.
* Switch back to the Pythonwin window - you should see:
IOleCommandTarget::Exec called.
This is the button being clicked. Extending this to do something more
useful is left as an exercise.
Contribtions to this sample to make it a little "friendlier" welcome!
"""
# imports section
import pythoncom
import win32api
import win32com
import win32com.server.register
from win32com import universal
from win32com.client import Dispatch, DispatchWithEvents, constants, gencache, getevents
# This demo uses 'print' - use win32traceutil to see it if we have no
# console.
try:
win32api.GetConsoleTitle()
except win32api.error:
import win32traceutil
import array
from win32com.axcontrol import axcontrol
# ensure we know the ms internet controls typelib so we have access to IWebBrowser2 later on
win32com.client.gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
#
IObjectWithSite_methods = ["SetSite", "GetSite"]
IOleCommandTarget_methods = ["Exec", "QueryStatus"]
_iebutton_methods_ = IOleCommandTarget_methods + IObjectWithSite_methods
_iebutton_com_interfaces_ = [
axcontrol.IID_IOleCommandTarget,
axcontrol.IID_IObjectWithSite, # IObjectWithSite
]
class Stub:
"""
this class serves as a method stub,
outputting debug info whenever the object
is being called.
"""
def __init__(self, name):
self.name = name
def __call__(self, *args):
print("STUB: ", self.name, args)
class IEButton:
"""
The actual COM server class
"""
_com_interfaces_ = _iebutton_com_interfaces_
_public_methods_ = _iebutton_methods_
_reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER
_button_text_ = "IE Button"
_tool_tip_ = "An example implementation for an IE Button."
_icon_ = ""
_hot_icon_ = ""
def __init__(self):
# put stubs for non-implemented methods
for method in self._public_methods_:
if not hasattr(self, method):
print("providing default stub for %s" % method)
setattr(self, method, Stub(method))
def QueryStatus(self, pguidCmdGroup, prgCmds, cmdtextf):
# 'cmdtextf' is the 'cmdtextf' element from the OLECMDTEXT structure,
# or None if a NULL pointer was passed.
result = []
for id, flags in prgCmds:
flags |= axcontrol.OLECMDF_SUPPORTED | axcontrol.OLECMDF_ENABLED
result.append((id, flags))
if cmdtextf is None:
cmdtext = None # must return None if nothing requested.
# IE never seems to want any text - this code is here for
# demo purposes only
elif cmdtextf == axcontrol.OLECMDTEXTF_NAME:
cmdtext = "IEButton Name"
else:
cmdtext = "IEButton State"
return result, cmdtext
def Exec(self, pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn):
print(pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn)
print("IOleCommandTarget::Exec called.")
# self.webbrowser.ShowBrowserBar(GUID_IETOOLBAR, not is_ietoolbar_visible())
def SetSite(self, unknown):
if unknown:
# first get a command target
cmdtarget = unknown.QueryInterface(axcontrol.IID_IOleCommandTarget)
# then travel over to a service provider
serviceprovider = cmdtarget.QueryInterface(pythoncom.IID_IServiceProvider)
# finally ask for the internet explorer application, returned as a dispatch object
self.webbrowser = win32com.client.Dispatch(
serviceprovider.QueryService(
"{0002DF05-0000-0000-C000-000000000046}", pythoncom.IID_IDispatch
)
)
else:
# lose all references
self.webbrowser = None
def GetClassID(self):
return self._reg_clsid_
def register(classobj):
import winreg
subKeyCLSID = (
"SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s"
% classobj._reg_clsid_
)
try:
hKey = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
subKey = winreg.SetValueEx(
hKey, "ButtonText", 0, winreg.REG_SZ, classobj._button_text_
)
winreg.SetValueEx(
hKey, "ClsidExtension", 0, winreg.REG_SZ, classobj._reg_clsid_
) # reg value for calling COM object
winreg.SetValueEx(
hKey, "CLSID", 0, winreg.REG_SZ, "{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}"
) # CLSID for button that sends command to COM object
winreg.SetValueEx(hKey, "Default Visible", 0, winreg.REG_SZ, "Yes")
winreg.SetValueEx(hKey, "ToolTip", 0, winreg.REG_SZ, classobj._tool_tip_)
winreg.SetValueEx(hKey, "Icon", 0, winreg.REG_SZ, classobj._icon_)
winreg.SetValueEx(hKey, "HotIcon", 0, winreg.REG_SZ, classobj._hot_icon_)
except OSError:
print("Couldn't set standard toolbar reg keys.")
else:
print("Set standard toolbar reg keys.")
def unregister(classobj):
import winreg
subKeyCLSID = (
"SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s"
% classobj._reg_clsid_
)
try:
hKey = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
subKey = winreg.DeleteValue(hKey, "ButtonText")
winreg.DeleteValue(hKey, "ClsidExtension") # for calling COM object
winreg.DeleteValue(hKey, "CLSID")
winreg.DeleteValue(hKey, "Default Visible")
winreg.DeleteValue(hKey, "ToolTip")
winreg.DeleteValue(hKey, "Icon")
winreg.DeleteValue(hKey, "HotIcon")
winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
except OSError:
print("Couldn't delete Standard toolbar regkey.")
else:
print("Deleted Standard toolbar regkey.")
#
# test implementation
#
class PyWin32InternetExplorerButton(IEButton):
_reg_clsid_ = "{104B66A9-9E68-49D1-A3F5-94754BE9E0E6}"
_reg_progid_ = "PyWin32.IEButton"
_reg_desc_ = "Test Button"
_button_text_ = "IE Button"
_tool_tip_ = "An example implementation for an IE Button."
_icon_ = ""
_hot_icon_ = _icon_
def DllRegisterServer():
register(PyWin32InternetExplorerButton)
def DllUnregisterServer():
unregister(PyWin32InternetExplorerButton)
if __name__ == "__main__":
win32com.server.register.UseCommandLine(
PyWin32InternetExplorerButton,
finalize_register=DllRegisterServer,
finalize_unregister=DllUnregisterServer,
)