[python-win32] Writing event handlers

Tim Roberts timr at probo.com
Thu Aug 30 13:11:26 EDT 2018


Kairen Tan wrote:
>
> I’m trying to write some python to consume events from an Event 
> Handler….without much success.
>
> I’m using a class created using makepy, and on inspecting the file, 
> there is a block of commented code like:
>
> # Event Handlers
> # If you create handlers, they should have the following prototypes:
> #def OnBeforeOpen(self, oRecord=defaultNamedNotOptArg):
> #def OnBeforeSave(self, oRecord=defaultNamedNotOptArg, 
> bCancel=defaultNamedNotOptArg):
> #def OnAfterSave(self, oRecord=defaultNamedNotOptArg):
> #def OnBeforeDelete(self, oRecord=defaultNamedNotOptArg, 
> bCancel=defaultNamedNotOptArg):
> #def OnCloseRecord(self):
> #def OnAfterDelete(self, lDatabaseID=defaultNamedNotOptArg, 
> sImportID=defaultNamedNotOptArg):
>
> *My question is simply, how do I implement this? *
> *
> *
> I’ve tried using win32com.server.register.RegisterServer, but I’m 
> stumped as to what comes after that.

It would have been helpful if you had told us what server you were 
trying to work with here.

You don't need to register yourself as a server.  Registration is only 
needed if someone else is going to look for your object by its CLSID, 
but that's not the case here.  There's really no such thing as an "event 
handler" in COM.  I'm guessing that the server you're working with wants 
you to hand it a COM interface, and it will call methods of that COM 
interface when certain events occur.

So, you need to use the magic _reg_ names so that win32com recognizes 
you as a COM object, but after that, all you should need to do is create 
an instance of your object, and pass it to whatever method your server 
uses to register the callback object.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3980 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-win32/attachments/20180830/dd6bf60b/attachment.bin>


More information about the python-win32 mailing list