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.

win32gui_devicenotify.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Demo RegisterDeviceNotification etc. Creates a hidden window to receive
  2. # notifications. See serviceEvents.py for an example of a service doing
  3. # that.
  4. import sys
  5. import time
  6. import win32api
  7. import win32con
  8. import win32file
  9. import win32gui
  10. import win32gui_struct
  11. import winnt
  12. # These device GUIDs are from Ioevent.h in the Windows SDK. Ideally they
  13. # could be collected somewhere for pywin32...
  14. GUID_DEVINTERFACE_USB_DEVICE = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
  15. # WM_DEVICECHANGE message handler.
  16. def OnDeviceChange(hwnd, msg, wp, lp):
  17. # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure,
  18. # using the self-identifying data inside the DEV_BROADCAST_HDR.
  19. info = win32gui_struct.UnpackDEV_BROADCAST(lp)
  20. print("Device change notification:", wp, str(info))
  21. if (
  22. wp == win32con.DBT_DEVICEQUERYREMOVE
  23. and info.devicetype == win32con.DBT_DEVTYP_HANDLE
  24. ):
  25. # Our handle is stored away in the structure - just close it
  26. print("Device being removed - closing handle")
  27. win32file.CloseHandle(info.handle)
  28. # and cancel our notifications - if it gets plugged back in we get
  29. # the same notification and try and close the same handle...
  30. win32gui.UnregisterDeviceNotification(info.hdevnotify)
  31. return True
  32. def TestDeviceNotifications(dir_names):
  33. wc = win32gui.WNDCLASS()
  34. wc.lpszClassName = "test_devicenotify"
  35. wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
  36. wc.hbrBackground = win32con.COLOR_WINDOW + 1
  37. wc.lpfnWndProc = {win32con.WM_DEVICECHANGE: OnDeviceChange}
  38. class_atom = win32gui.RegisterClass(wc)
  39. hwnd = win32gui.CreateWindow(
  40. wc.lpszClassName,
  41. "Testing some devices",
  42. # no need for it to be visible.
  43. win32con.WS_CAPTION,
  44. 100,
  45. 100,
  46. 900,
  47. 900,
  48. 0,
  49. 0,
  50. 0,
  51. None,
  52. )
  53. hdevs = []
  54. # Watch for all USB device notifications
  55. filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
  56. GUID_DEVINTERFACE_USB_DEVICE
  57. )
  58. hdev = win32gui.RegisterDeviceNotification(
  59. hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE
  60. )
  61. hdevs.append(hdev)
  62. # and create handles for all specified directories
  63. for d in dir_names:
  64. hdir = win32file.CreateFile(
  65. d,
  66. winnt.FILE_LIST_DIRECTORY,
  67. winnt.FILE_SHARE_READ | winnt.FILE_SHARE_WRITE | winnt.FILE_SHARE_DELETE,
  68. None, # security attributes
  69. win32con.OPEN_EXISTING,
  70. win32con.FILE_FLAG_BACKUP_SEMANTICS
  71. | win32con.FILE_FLAG_OVERLAPPED, # required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME.
  72. None,
  73. )
  74. filter = win32gui_struct.PackDEV_BROADCAST_HANDLE(hdir)
  75. hdev = win32gui.RegisterDeviceNotification(
  76. hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE
  77. )
  78. hdevs.append(hdev)
  79. # now start a message pump and wait for messages to be delivered.
  80. print("Watching", len(hdevs), "handles - press Ctrl+C to terminate, or")
  81. print("add and remove some USB devices...")
  82. if not dir_names:
  83. print("(Note you can also pass paths to watch on the command-line - eg,")
  84. print("pass the root of an inserted USB stick to see events specific to")
  85. print("that volume)")
  86. while 1:
  87. win32gui.PumpWaitingMessages()
  88. time.sleep(0.01)
  89. win32gui.DestroyWindow(hwnd)
  90. win32gui.UnregisterClass(wc.lpszClassName, None)
  91. if __name__ == "__main__":
  92. # optionally pass device/directory names to watch for notifications.
  93. # Eg, plug in a USB device - assume it connects as E: - then execute:
  94. # % win32gui_devicenotify.py E:
  95. # Then remove and insert the device.
  96. TestDeviceNotifications(sys.argv[1:])