Advertisement
Guest User

win32gui_devicenotify.py

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