I need to dynamically create com objects from an activex dll and each of the objects can raise events which should be handled with event handlers.
I can do this easily with win32com.client.Dispatch
and win32com.client.WithEvents
and associate a "separate" class of event handlers with each of the objects. Like so:
class evt_1:
def OnEvent(self):
print "got event from object 1"
class evt_2:
def OnEvent(self):
print "got event from object 2"
obj_1 = win32com.client.Dispatch("mycom")
ev_1 = win32com.client.WithEvents(obj_1, evt_1)
obj_2 = win32com.client.Dispatch("mycom")
ev_1 = win32com.client.WithEvents(obj_2, evt_2)
But if I dynamically create the objects, lets say in a list:
listOfObjects = []
for i in range(10):
obj = win32com.client.Dispatch("mycom")
listOfObjects.append(obj)
ev = win32com.client.WithEvents(obj, MyEventHandlerClass)
I want to code the event handlers only once, since I don't know how many objects I would be creating until run time. And I don't know how to get the object that raised the event from inside the event handler.
In VB 6, I've used the activex control using control arrays, and the event handlers simply get an "index" value of the control that raised the event.
Do you think something similar can be done in python ?
I am not sure what python decorators work for, but can they be used to "decorate" the MyEventHandlerClass for each index of the com object?