Events in win32com

Alex Martelli aleaxit at yahoo.com
Tue Dec 19 12:11:16 EST 2000


"Paul Moore" <paul.moore at uk.origin-it.com> wrote in message
news:Y1g=Op6IfWSoJn1ISFtOyuvqwBb3 at 4ax.com...
> If I create an object using
>
> obj = win32com.client.Dispatch(progid)
>
> can I see events from the object?

You can, although using DispatchWithEvents may be easier.

This is a typical example...:

import win32com.client
from win32com.client import gencache
evmod = gencache.EnsureModule('{D240EC61-B6ED-11D4-9E45-0060B0EB1D67}', 0,
1, 0)

class Evhand:
    def OnOne(self):
        print "eh: One!"
    def OnTwo(self):
        print "eh: Two!"

class Other(evmod._IEvGenEvents2):
    def OnOne(self):
        print "oh: One!"
    def OnTwo(self):
        print "oh: Two!"

ob = win32com.client.DispatchWithEvents("evts.EvGen", Evhand)
oh = Other(ob)
ob.Fire()


where progid 'evts.EvGen' refers to a tiny object I built just
for the purpose of event-handling tests (it generates events
along different sourceinterfaces when you call its Fire method).
This shows we can get events separately from DispatchWithEvents
(and on a different sourceinterface than the default one, if we
know the name of that alternate sourceinterface!).

> In WSH the CreateObject method has
> an extra parameter which is the prefix for subroutine names which are
> called on an event, something like
>
> ie = WScript.CreateObject("InternetExplorer.Application", "ie_")
>
> Sub ie_OnQuit()
> End Sub
>
> While I don't like the "specially named subroutine" approach, I do
> need to be able to sink events in Python. Is this possible, or should
> I resort to using Python via WSH?

It's quite possible to avoid WSH, unless you like the services
it brings to your script.


> (BTW, assuming I can sink events, will Python handle ByRef event
> parameters - for example, a Cancel parameter which is set to True in
> the handler to stop the event occurring).

If your EnsureModule (or equivalent) has been called, so that
Python *knows* exactly what's happening, then, yes, [out] and
[in,out] parameters will be handled (COM has no 'byref', but
that's how VB maps [in,out] -- [out] is a big problem in VB),
but of course they'll be handled for events as in other COM
Python cases -- that is, they become extra _return values_
of your method.


Alex






More information about the Python-list mailing list