>> >> You could try comtypes: it should support custom interfaces and
>> >> custom event interfaces.
>> >>
>> >> 'easy_install comtypes==dev' to get the SVN version.
>> >>
> > Wow. The battle of the packages is afoot :-)
> > Thanks for the hint: I did not know about this package.
> > I have given it a try. There are a few things that appear strange to me.
> > At first I tried this:
> > 
> > import comtypes.client as cc
> > class testVehikel(cc.CreateObject(progid), eventsClass):
> >     def __init__(self):
> >     (...)  
> > 
> > This produced the following exception:
> >     class testVehikel(cc.CreateObject(progid), eventsClass):
> > TypeError: Error when calling the metaclass bases
> >     __init__ expected at most 1 arguments, got 3
> > 
> > I have no clue where this comes from, not even after trying to step towards 
> > the 
> > error. Does anyone?

Strange? What do you mean, your own code ;-) ?
CreateObject() creates an instance of a COM object and returns an
interface pointer to it.  You can use that directly to call COM methods
or access COM properties - you cannot use this thing as a base class for
another class: that's why you get this metaclass error.

> > So I did it slightly different:
> > 
> > import comtypes.client as cc
> > import comtypes.gen.S7PROSIMLib as S7PS
> > class testVehikel:
> >     def __init__(self, progid, eventsClass):
> >         self.intf = cc.CreateObject(progid)
> >         self.cb = eventsClass
> >         cc.GetEvents(self.intf, self.cb, S7PS.IS7ProSimEvents)
> >         (...)
> >     (.... other methods ....)
> >     def __del__(self):
> >         (.... some cleaning up ....)
> >       
> > This runs, except that the __del__ method gets called before I expect it, 
> > and 
> > after it is called the other testVehikel  methods still run fine.
> > How is this possible?

This looks a little bit better.  Basically you do:

intf = cc.CreateObject(progid)
con = cc.GetEvents(intf, eventreceiver, event_interface)

The last parameter to GetEvents is optional, some COM objects expose enough
information so that comtypes can determine the default source interface itself.

Then, you have to keep the object that GetEvent() returns because when this 
object
goes away the advise connection is closed and cannot receive events any more.

Third, different from the pywin32 way, you pass an object as eventreceiver, NOT
a class.  The eventreceiver must have methods (event handlers) corresponding to 
the events that
it receives.  events that have no handlers are silently ignored.

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.

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 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.

Thomas


_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to