コード例 #1
0
 def testPackUnpack(self):
     s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
         pythoncom.IID_IUnknown, "hello")
     c = array.array("b", s)
     got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
     self.failUnlessEqual(got.classguid, pythoncom.IID_IUnknown)
     self.failUnlessEqual(got.name, "hello")
コード例 #2
0
 def testPackUnpack(self):
     s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
         pythoncom.IID_IUnknown, "hello")
     c = array.array("b", s)
     got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
     assert got.classguid == pythoncom.IID_IUnknown
     assert got.name == "hello"
コード例 #3
0
def TestDeviceNotifications(dir_names):
    wc = win32gui.WNDCLASS()
    wc.lpszClassName = 'test_devicenotify'
    wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
    wc.hbrBackground = win32con.COLOR_WINDOW + 1
    wc.lpfnWndProc = {win32con.WM_DEVICECHANGE: OnDeviceChange}
    class_atom = win32gui.RegisterClass(wc)
    hwnd = win32gui.CreateWindow(
        wc.lpszClassName,
        'Testing some devices',
        # no need for it to be visible.
        win32con.WS_CAPTION,
        100,
        100,
        900,
        900,
        0,
        0,
        0,
        None)

    hdevs = []
    # Watch for all USB device notifications
    filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
        GUID_DEVINTERFACE_USB_DEVICE)
    hdev = win32gui.RegisterDeviceNotification(
        hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE)
    hdevs.append(hdev)
    # and create handles for all specified directories
    for d in dir_names:
        hdir = win32file.CreateFile(
            d,
            winnt.FILE_LIST_DIRECTORY,
            winnt.FILE_SHARE_READ | winnt.FILE_SHARE_WRITE
            | winnt.FILE_SHARE_DELETE,
            None,  # security attributes
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS
            |  # required privileges: SE_BACKUP_NAME and SE_RESTORE_NAME.
            win32con.FILE_FLAG_OVERLAPPED,
            None)

        filter = win32gui_struct.PackDEV_BROADCAST_HANDLE(hdir)
        hdev = win32gui.RegisterDeviceNotification(
            hwnd, filter, win32con.DEVICE_NOTIFY_WINDOW_HANDLE)
        hdevs.append(hdev)

    # now start a message pump and wait for messages to be delivered.
    print "Watching", len(hdevs), "handles - press Ctrl+C to terminate, or"
    print "add and remove some USB devices..."
    if not dir_names:
        print "(Note you can also pass paths to watch on the command-line - eg,"
        print "pass the root of an inserted USB stick to see events specific to"
        print "that volume)"
    while 1:
        win32gui.PumpWaitingMessages()
        time.sleep(0.01)
    win32gui.DestroyWindow(hwnd)
    win32gui.UnregisterClass(wc.lpszClassName, None)
コード例 #4
0
 def __init__(self,args):
     win32serviceutil.ServiceFramework.__init__(self,args)
     self.stopEvent = threading.Event()
     
     FILTER = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
                                GUID_DEVINTERFACE_USB_DEVICE)
     self.hDevNotify = win32gui.RegisterDeviceNotification(self.ssh,
                                FILTER,win32con.DEVICE_NOTIFY_SERVICE_HANDLE)
コード例 #5
0
 def __init__(self, args):
     win32serviceutil.ServiceFramework.__init__(self, args)
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
     # register for a device notification - we pass our service handle
     # instead of a window handle.
     filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
         GUID_DEVINTERFACE_USB_DEVICE)
     self.hdn = win32gui.RegisterDeviceNotification(
         self.ssh, filter, win32con.DEVICE_NOTIFY_SERVICE_HANDLE)
コード例 #6
0
    def __init__(self, device=hid.Device, vid=None, pid=None, path=None):
        # Initialize the base class event object
        super().__init__()
    
        # Path must be convertable to bytes, and vid/pid integers
        if path:
            try:
                self.__path = path.lower().encode()
            except AttributeError:
                self.__path = path.lower()
            try:
                with hid.Device(path=self.__path):
                    super().set()
            except hid.HIDException:
                pass
        elif vid and pid:
            try:
                v, p = map(lambda x: int(x),[vid,pid])
            except:
                v, p = map(lambda x: int(x, 0),[vid,pid])
            
            # The IDs are only used to find the full device path
            self.__ids = list(f"{x}_{y:04x}".encode() for x, y in zip(["vid","pid"],[v,p]))
            
            # Always use path for connection; check for device at startup
            self.__path = None
            for d in hid.enumerate(vid=v, pid=p):
                if self.__matchingdevice(d["path"]):
                    super().set()
                    break
        else:
            raise Exception("VID, PID or path required for HID device")
        
        self.__dev = device
    
        # Create a window class for receiving messages
        self.wc = win32gui.WNDCLASS()
        self.wc.hInstance = win32api.GetModuleHandle(None)
        self.wc.lpszClassName = "win32hidnotifier"
        self.wc.lpfnWndProc = {win32con.WM_DEVICECHANGE:self.__devicechange}
        
        # Register the window class
        winClass = win32gui.RegisterClass(self.wc)
        
        # Create a Message-Only window
        self.hwnd = win32gui.CreateWindowEx(
            0,                      #dwExStyle
			self.wc.lpszClassName,  #lpClassName
			self.wc.lpszClassName,  #lpWindowName
			0, 0, 0, 0, 0,
			win32con.HWND_MESSAGE,  #hWndParent
			0, 0, None)
        
        # Watch for all USB device notifications
        self.filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(Win32HID.GUID_DEVINTERFACE_HID)
        self.hdev = win32gui.RegisterDeviceNotification(self.hwnd, self.filter,
                                                        win32con.DEVICE_NOTIFY_WINDOW_HANDLE)
コード例 #7
0
ファイル: detector.py プロジェクト: benny-z/FlashBackup
 def __init__(self, args):
     win32serviceutil.ServiceFramework.__init__(self, args)
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
     #
     # Specify that we're interested in device interface
     # events for USB devices
     #
     filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
         GUID_DEVINTERFACE_USB_DEVICE)
     self.hDevNotify = win32gui.RegisterDeviceNotification(
         self.ssh,  # copy of the service status handle
         filter,
         win32con.DEVICE_NOTIFY_SERVICE_HANDLE)
コード例 #8
0
ファイル: OPEService.py プロジェクト: mdrafikrft/ope
    def ListenForDeviceEvents(self):

        try:
            # register for a device notification - we pass our service handle
            # instead of a window handle.
            filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
                OPEService.GUID_DEVINTERFACE_USB_DEVICE)
            self.hdn = win32gui.RegisterDeviceNotification(
                self.ssh, filter, win32con.DEVICE_NOTIFY_SERVICE_HANDLE)

            p("}}cnService now listening for device events", log_level=3)
        except Exception as ex:
            p("Unknown Error listening for device events " + str(ex),
              log_level=1)

        return
コード例 #9
0
ファイル: OPEService.py プロジェクト: kyletlm/ope
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

        socket.setdefaulttimeout(60)
        self.isAlive = True

        # Setup data folders and set permissions
        set_ope_permissions()

        # Setup logging
        logging.basicConfig(
            filename=os.path.join(LOG_FOLDER, 'ope-service.log'),
            level=logging.DEBUG,
            format='[ope-service] %(levelname)-7.7s %(message)s')
        logging.info("service init")

        # register for a device notification - we pass our service handle
        # instead of a window handle.
        filter = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
            GUID_DEVINTERFACE_USB_DEVICE)
        self.hdn = win32gui.RegisterDeviceNotification(
            self.ssh, filter, win32con.DEVICE_NOTIFY_SERVICE_HANDLE)
コード例 #10
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')