Esempio n. 1
0
def OnDeviceChange(hwnd, msg, wp, lp):
    # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure,
    # using the self-identifying data inside the DEV_BROADCAST_HDR.
    info = win32gui_struct.UnpackDEV_BROADCAST(lp)
    #print "Device change notification:", wp, str(info)
    #print str(info)
    #print myDevice
    if wp != 7:

        if wp == 32768:
            print("Device inserted")
            if str(info) == myDevice:
                print("Our Device detected")
                abcd = subprocess.Popen("notepad.exe")
            else:
                print("its not our device")
        else:
            global abcd
            abcd.terminate()
            print("Device removed")

    if wp == win32con.DBT_DEVICEQUERYREMOVE and info.devicetype == win32con.DBT_DEVTYP_HANDLE:
        # Our handle is stored away in the structure - just close it
        print("Device being removed - closing handle")
        win32file.CloseHandle(info.handle)
        # and cancel our notifications - if it gets plugged back in we get
        # the same notification and try and close the same handle...
        win32gui.UnregisterDeviceNotification(info.hdevnotify)
    return True
Esempio n. 2
0
def OnDeviceChange(hwnd, msg, wp, lp):
    # Unpack the 'lp' into the appropriate DEV_BROADCAST_* structure,
    # using the self-identifying data inside the DEV_BROADCAST_HDR.
    info = win32gui_struct.UnpackDEV_BROADCAST(lp)
    print "Device change notification:", wp, str(info)
    if wp == win32con.DBT_DEVICEQUERYREMOVE and info.devicetype == win32con.DBT_DEVTYP_HANDLE:
        # Our handle is stored away in the structure - just close it
        print "Device being removed - closing handle"
        win32file.CloseHandle(info.handle)
        # and cancel our notifications - if it gets plugged back in we get
        # the same notification and try and close the same handle...
        win32gui.UnregisterDeviceNotification(info.hdevnotify)
    return True
Esempio n. 3
0
    def _run_window(self):
        # Create hidden window
        log.debug('_run_window start')
        wc = win32gui.WNDCLASS()
        wc.lpszClassName = 'devicenotify'
        wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
        wc.hbrBackground = win32con.COLOR_WINDOW + 1
        wc.lpfnWndProc = self.window_callback
        class_atom = win32gui.RegisterClass(wc)
        if not class_atom:
            log.error('window class not created')
            return
        self._hwnd = win32gui.CreateWindow(wc.lpszClassName, 'devicenotify',
                                           win32con.WS_CAPTION, 100, 100, 900,
                                           900, 0, 0, 0, None)
        if not self._hwnd:
            log.error('window not created')
            return

        # Watch for all USB device notifications
        devfilt = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
            GUID_DEVINTERFACE_USB_DEVICE)
        self._event = win32gui.RegisterDeviceNotification(
            self._hwnd, devfilt, win32con.DEVICE_NOTIFY_WINDOW_HANDLE)

        while 1:
            b, msg = win32gui.GetMessage(None, 0, 0)
            log.debug('win32_device_notify message')
            if not b or not msg:
                break
            win32gui.TranslateMessage(msg)
            win32gui.DispatchMessage(msg)
        win32gui.UnregisterDeviceNotification(self._event)
        win32gui.UnregisterClass(wc.lpszClassName, None)
        self._hwnd = None
        log.debug('_run_window done')