-
Notifications
You must be signed in to change notification settings - Fork 4
/
com.py
executable file
·150 lines (116 loc) · 3.95 KB
/
com.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import time
import signal
try:
import pywintypes
import win32com.client
import win32com.client.gencache
except:
pass
from Peach.publisher import Publisher
class Com(Publisher):
"""
Very simple Com publisher that allows for a single method call. The
method call is fed in as a string which is evaled. This allows for
calling any method with any number of parameters.
"""
_clsid = None
_methodFormat = None
_lastReturn = None
_object = None
def __init__(self, clsid):
"""
Create Com Object. clsid = '{...}'
@type clsid: string
@param clsid: CLSID of COM object in {...} format
"""
Publisher.__init__(self)
self._clsid = clsid
self.withNode = True
def start(self):
try:
self._object = None
self._object = win32com.client.DispatchEx(self._clsid)
except pywintypes.com_error as e:
print("Caught pywintypes.com_error creating ActiveX control [%s]" % e)
raise
except:
print("Caught unkown exception creating ActiveX control [%s]" % sys.exc_info()[0])
raise
def stop(self):
self._object = None
# def call(self, method, args):
def callWithNode(self, method, args, argNodes):
"""
Call method on COM object.
@type method: string
@param method: Name of method to invoke
@type args: array of objects
@param args: Arguments to pass
"""
self._lastReturn = None
realArgNodes = []
for arg in argNodes:
if len(arg) == 1:
realArgNodes.append(arg[0])
else:
realArgNodes.append(arg)
for arg in realArgNodes:
print("Type", type(arg.getInternalValue()))
print("Value", repr(arg.getInternalValue()))
try:
ret = None
callStr = "ret = self._object.%s(" % str(method)
if len(realArgNodes) > 0:
for i in range(0, len(argNodes)):
callStr += "realArgNodes[%d].getInternalValue()," % i
callStr = callStr[:len(callStr) - 1] + ")"
else:
callStr += ")"
print("Call:", callStr)
exec(callStr)
return ret
except pywintypes.com_error as e:
print("Caught pywintypes.com_error on call [%s]" % e)
raise
except NameError as e:
print("Caught NameError on call [%s]" % e)
raise
except:
print("Com::Call(): Caught unknown exception")
raise
# def property(self, property, value = None):
def propertyWithNode(self, property, value, valueNode):
"""
Get or set property
@type property: string
@param property: Name of method to invoke
@type value: object
@param value: Value to set. If None, return property instead
"""
try:
if value is None:
ret = None
callStr = "ret = self._object.%s" % str(property)
#print "Call string:", callStr
exec(callStr)
return ret
ret = None
callStr = "self._object.%s = valueNode.getInternalValue()" % str(property)
#print "Call string:", callStr
exec(callStr)
return None
except pywintypes.com_error as e:
print("Caught pywintypes.com_error on property [%s]" % e)
#raise
except NameError as e:
print("Caught NameError on property [%s]" % e)
#raise
except:
print("Com::property(): Caught unknown exception")
raise
return None