[python-win32] fix: GetIDsOfNames failed in event handler

Koen Van Herck koen_van_herck at yahoo.com
Fri Nov 7 06:35:22 EST 2003


Hi,

I have encountered the following problem and was able to fix it. Please let
me know if there are maybe better ways to fix the problem.

First I create an object using Dispatch. Calling a method on this object
returns me another object which implements events. So I've defined a class
MyEventsThing, derived from makepy's generated IEventsThing class, which
defines the event method OnSomeEvent. Then the event happens and the COM
object tries to call my event handler using GetIDsOfNames with a name of
SomeEvent. This fails because the _name_to_dispid_ table of the
DesignatedWrapPolicy only contains OnSomeEvent.

There are two things strange here: first, I wondered why I got
DesignatedWrapPolicy instance instead of an EventHandlerPolicy instance. I
could fix this by redefining the _query_interface_ method in my
MyEventsThing class:

def _query_interface_(self, iid):
    if iid==self.CLSID_Sink: return win32com.server.util.wrap(self,
usePolicy=EventHandlerPolicy)

Is this maybe a bug in the makepy generated file (it doesn't specify the
usePolicy) ?

Once I fixed this, I added a _wrap_ method in the EventHandlerPolicy class:

def _wrap_(self, ob):
    win32com.server.policy.EventHandlerPolicy._wrap_(self, ob)
    # Copy existing _dispid_to_func_ 'OnSomeEvent' entries
    # to _name_to_dispid_ 'SomeEvent' entries
    for dispid, name in self._dispid_to_func_.items():
        if name[0:2].lower() == 'on':
            self._name_to_dispid_[name[2:].lower()]=dispid

This will expose my OnSomeEvent method as SomeEvent, and everything works
fine.

Regards,
Koen Van Herck.




More information about the Python-win32 mailing list