12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import win32serviceutil, win32service
- import win32event
- import servicemanager
- import win32gui, win32gui_struct, win32con
- GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
- class EventDemoService(win32serviceutil.ServiceFramework):
- _svc_name_ = "PyServiceEventDemo"
- _svc_display_name_ = "Python Service Event Demo"
- _svc_description_ = "Demonstrates a Python service which takes advantage of the extra notifications"
- def __init__(self, args):
- win32serviceutil.ServiceFramework.__init__(self, args)
- self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
-
-
- filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
- GUID_DEVINTERFACE_USB_DEVICE)
- self.hdn = win32gui.RegisterDeviceNotification(self.ssh, filter,
- win32con.DEVICE_NOTIFY_SERVICE_HANDLE)
-
- def GetAcceptedControls(self):
-
- rc = win32serviceutil.ServiceFramework.GetAcceptedControls(self)
- rc |= win32service.SERVICE_ACCEPT_PARAMCHANGE \
- | win32service.SERVICE_ACCEPT_NETBINDCHANGE \
- | win32service.SERVICE_CONTROL_DEVICEEVENT \
- | win32service.SERVICE_ACCEPT_HARDWAREPROFILECHANGE \
- | win32service.SERVICE_ACCEPT_POWEREVENT \
- | win32service.SERVICE_ACCEPT_SESSIONCHANGE
- return rc
-
-
- def SvcOtherEx(self, control, event_type, data):
-
-
- if control == win32service.SERVICE_CONTROL_DEVICEEVENT:
- info = win32gui_struct.UnpackDEV_BROADCAST(data)
- msg = "A device event occurred: %x - %s" % (event_type, info)
- elif control == win32service.SERVICE_CONTROL_HARDWAREPROFILECHANGE:
- msg = "A hardware profile changed: type=%s, data=%s" % (event_type, data)
- elif control == win32service.SERVICE_CONTROL_POWEREVENT:
- msg = "A power event: setting %s" % data
- elif control == win32service.SERVICE_CONTROL_SESSIONCHANGE:
-
-
- msg = "Session event: type=%s, data=%s" % (event_type, data)
- else:
- msg = "Other event: code=%d, type=%s, data=%s" \
- % (control, event_type, data)
- servicemanager.LogMsg(
- servicemanager.EVENTLOG_INFORMATION_TYPE,
- 0xF000,
- (msg, '')
- )
-
- def SvcStop(self):
- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
- win32event.SetEvent(self.hWaitStop)
- def SvcDoRun(self):
-
- win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
-
- servicemanager.LogMsg(
- servicemanager.EVENTLOG_INFORMATION_TYPE,
- servicemanager.PYS_SERVICE_STOPPED,
- (self._svc_name_, '')
- )
- if __name__=='__main__':
- win32serviceutil.HandleCommandLine(EventDemoService)
|