Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serviceEvents.py 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # A Demo of a service that takes advantage of the additional notifications
  2. # available in later Windows versions.
  3. # Note that all output is written as event log entries - so you must install
  4. # and start the service, then look at the event log for messages as events
  5. # are generated.
  6. # Events are generated for USB device insertion and removal, power state
  7. # changes and hardware profile events - so try putting your computer to
  8. # sleep and waking it, inserting a memory stick, etc then check the event log
  9. # Most event notification support lives around win32gui
  10. import servicemanager
  11. import win32con
  12. import win32event
  13. import win32gui
  14. import win32gui_struct
  15. import win32service
  16. import win32serviceutil
  17. GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
  18. class EventDemoService(win32serviceutil.ServiceFramework):
  19. _svc_name_ = "PyServiceEventDemo"
  20. _svc_display_name_ = "Python Service Event Demo"
  21. _svc_description_ = (
  22. "Demonstrates a Python service which takes advantage of the extra notifications"
  23. )
  24. def __init__(self, args):
  25. win32serviceutil.ServiceFramework.__init__(self, args)
  26. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  27. # register for a device notification - we pass our service handle
  28. # instead of a window handle.
  29. filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
  30. GUID_DEVINTERFACE_USB_DEVICE
  31. )
  32. self.hdn = win32gui.RegisterDeviceNotification(
  33. self.ssh, filter, win32con.DEVICE_NOTIFY_SERVICE_HANDLE
  34. )
  35. # Override the base class so we can accept additional events.
  36. def GetAcceptedControls(self):
  37. # say we accept them all.
  38. rc = win32serviceutil.ServiceFramework.GetAcceptedControls(self)
  39. rc |= (
  40. win32service.SERVICE_ACCEPT_PARAMCHANGE
  41. | win32service.SERVICE_ACCEPT_NETBINDCHANGE
  42. | win32service.SERVICE_CONTROL_DEVICEEVENT
  43. | win32service.SERVICE_ACCEPT_HARDWAREPROFILECHANGE
  44. | win32service.SERVICE_ACCEPT_POWEREVENT
  45. | win32service.SERVICE_ACCEPT_SESSIONCHANGE
  46. )
  47. return rc
  48. # All extra events are sent via SvcOtherEx (SvcOther remains as a
  49. # function taking only the first args for backwards compat)
  50. def SvcOtherEx(self, control, event_type, data):
  51. # This is only showing a few of the extra events - see the MSDN
  52. # docs for "HandlerEx callback" for more info.
  53. if control == win32service.SERVICE_CONTROL_DEVICEEVENT:
  54. info = win32gui_struct.UnpackDEV_BROADCAST(data)
  55. msg = "A device event occurred: %x - %s" % (event_type, info)
  56. elif control == win32service.SERVICE_CONTROL_HARDWAREPROFILECHANGE:
  57. msg = "A hardware profile changed: type=%s, data=%s" % (event_type, data)
  58. elif control == win32service.SERVICE_CONTROL_POWEREVENT:
  59. msg = "A power event: setting %s" % data
  60. elif control == win32service.SERVICE_CONTROL_SESSIONCHANGE:
  61. # data is a single elt tuple, but this could potentially grow
  62. # in the future if the win32 struct does
  63. msg = "Session event: type=%s, data=%s" % (event_type, data)
  64. else:
  65. msg = "Other event: code=%d, type=%s, data=%s" % (control, event_type, data)
  66. servicemanager.LogMsg(
  67. servicemanager.EVENTLOG_INFORMATION_TYPE,
  68. 0xF000, # generic message
  69. (msg, ""),
  70. )
  71. def SvcStop(self):
  72. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  73. win32event.SetEvent(self.hWaitStop)
  74. def SvcDoRun(self):
  75. # do nothing at all - just wait to be stopped
  76. win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
  77. # Write a stop message.
  78. servicemanager.LogMsg(
  79. servicemanager.EVENTLOG_INFORMATION_TYPE,
  80. servicemanager.PYS_SERVICE_STOPPED,
  81. (self._svc_name_, ""),
  82. )
  83. if __name__ == "__main__":
  84. win32serviceutil.HandleCommandLine(EventDemoService)