The code below works fine. I can't find a way to pass some arguments to EventHandler or to call methods of MainClass from EventHandler. For example instead of using constant param, I'd like to pass it through constructor or setter method. I've tried recommendations from here. But in this case EventHandler instance does not catch any events (or at least nothing appears in stdout).
class EventHandler:
param = "value"
def OnConnected(self):
print 'connected'
return True
class MainClass:
def run(self):
pythoncom.CoInitialize()
session = win32com.client.Dispatch("Lib.Obj")
session_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, session)
args = { 's_id': session_id, }
thread = threading.Thread(target=self.run_in_thread, kwargs=args)
thread.start()
def run_in_thread(self, s_id):
pythoncom.CoInitialize()
session = win32com.client.DispatchWithEvent(
pythoncom.CoGetInterfaceAndReleaseStream(s_id, pythoncom.IID_IDispatch),
EventHandler
)
session.connect()
while True:
pythoncom.PumpWaitingMessages()
time.sleep(1)
if __name__ == '__main__':
obj = MainClass()
obj.run()