[python-win32] ctypes and python dll callbacks

le dahut le.dahut at laposte.net
Mon Jan 21 17:04:56 CET 2008

I'm trying to use RegisterServiceCtrlHandlerEx. Since it isn't available 
in pywin32, I have to call it from advapi32.dll using ctypes.
RegisterServiceCtrlHandlerEx is useful to replace winlogon notification 
package to get informations about login/logoff events. I could use SENS 
in a com server but I have already an NT service running a twisted 
PerspectiveBroker server.
A handler to a callback function must be passed to 
RegisterServiceCtrlHandlerEx. How can a python callback function handler 
be created to be passed to a ctypes DLL function ?

advapi32 = ctypes.oledll.LoadLibrary('Advapi32.dll')
RegisterServiceCtrlHandlerEx = advapi32.RegisterServiceCtrlHandlerExW
RegisterServiceCtrlHandlerEx(svc_name, myPyFunc, svc_name)

returns :
<class 'ctypes.ArgumentError'>: argument 2: <type 
'exceptions.TypeError'>: Don't know how to convert parameter 2

Any idea ?

More information about the python-win32 mailing list