[python-win32] win32com does not recognise an Interface for events

Thomas Heller theller at ctypes.org
Fri Aug 10 14:23:09 CEST 2007


Len Remmerswaal schrieb:
> 
> import comtypes.client as cc
> import comtypes.gen.S7PROSIMLib as S7PS
> class testVehikel:
>     def __init__(self, progid, eventsClass):
>         self.intf = cc.CreateObject(progid)
>         self.rcvr = eventsClass()   ## just added () to assign an obj i.o. a class
>         self.cb = cc.GetEvents(self.intf, self.rcvr, S7PS.IS7ProSimEvents)
>         (....)
> 
> I do need the interface definition in GetEvents or I get:  "TypeError: cannot 
> determine source interface". So be it.

Sure.  I just wanted to mention that often you do not have to do this.

>> Forth, comtypes has a useful function that will help exploring the events: ShowEvents().
>> This has the same signature and functionality as the GetEvents function, except that
>> you cannot pass the eventreceiver argument:  ShowEvents(obj [, interface]) constructs
>> a universal event receiver object itself.  This object will first simply print out
>> the event methods that it finds in the source interface, and then will print out
>> the event names and arguments when events are received.  Again, you must keep the
>> return value of the ShowEvents() call as long as you want to receive events.
>>   
> I suppose I'll have to download the development version for that: version 0.2.1 
> seems not to have ShowEvents.

Please download comtypes-0.3.2 which was released two days ago.

>> And finally, depending on the COM apartment your code runs in, and depending
>> on the COM object, you may need to run a windows MessageLoop so that events
>> are properly dispatched by COM.
>>   
> I'll try that: right now some events seem to occur only when a corresponding 
> dispatch routine is run, i.e. an event "Status changed" occurs if I call
> "getStatus". I saw an example of how to do this in comtypes\test\test_ie.py.
> What I wonder is how this message loop relates to the pywin message loop in 
> which I currently run this code, or perhaps the wxPython message loop where this 
> code will probably be running in later. BTW: the COM object resides in a DLL so 
> I would think it runs in-process.

IMO in-process or outof-process doesn't matter.
Any message loop should do:  Either the PumpWaitingMessage function in the
test_ie.py module, the pythoncom.PumpWaitingMessage funtion from pywin32,
the message loop that PythonWin uses if you run the code inside that, maybe
even a wxPython, Tkinter or IDLE messageloop.

In the development version of comtypes in SVN I just added the PumpWaitingMessages
function to the comtypes.client module.

> 
>>> I final question:
>>> What should the event routines look like? Currently they look like this:
>>>
>>> class testCalls2:
>>>     def ScanFinished(self, this, ScanInfo):
>>>         print "Event: ScanFinished: ", ScanInfo
>>>     (....)
>>>
>>> on the basis of <clsid>.py, the result of GetMessage, saying this:
>>>
>>> IS7ProSimEvents._methods_ = [
>>>     COMMETHOD([helpstring(u'Fired when single scan is done.')], HRESULT, 
>>> 'ScanFinished',
>>>               ( [], VARIANT, 'ScanInfo' )),
>>>     (....)
>>>
>>> I thought to have read somewhere that I need a (not further needed) 'this' 
>>> argument. Should I do it like this or should the 'this' argument go?
>>> So far I have not been able to trigger any of these routines and make them do 
>>> their printing.
>>>     
>>
>>
>> I think this is correct, the this parameter is needed
>>   
> Almost correct. All event routines must return an integer, or I get this:
> 
> Traceback (most recent call last):
>   File "source/callbacks.c", line 216, in 'converting callback result'
> TypeError: int expected instead of NoneType instance
> Exception  in <function func at 0x00EEF370> ignored
> 
> after which the thing continues happily.
> At first I tried returning 0 (ERROR_SUCCESS) as the HRESULT , but this hangs up 
> the code. Now I return 1 and it works fine.
> The odd thing is that the methods in EventSink in comtypes\test\test_ie.py do 
> not return anything. Is this a version problem (I use 0.2.1)?

I think so.  Please use 0.3.2: it is much improved.

BTW: You should probably subscribe to the new comtypes-users mailing list,
and move this discussion over there:
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Thomas



More information about the python-win32 mailing list