示例#1
0
    def ListDevices():
        devices = {}
        guid = GUID()
        CLSIDFromString("{A5DCBF10-6530-11D2-901F-00C04FB951ED}", byref(guid))
        hDevInfo = SetupDiGetClassDevs(
            guid,
            "USB",  # Enumerator
            0,
            DIGCF_PRESENT | DIGCF_ALLCLASSES)
        if hDevInfo == INVALID_HANDLE_VALUE:
            raise WinError()
        deviceInfoData = SP_DEVINFO_DATA()
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA)
        driverInfoData = SP_DRVINFO_DATA()
        driverInfoData.cbSize = sizeof(SP_DRVINFO_DATA)
        deviceInstallParams = SP_DEVINSTALL_PARAMS()
        deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS)

        i = 0
        while True:
            if not SetupDiEnumDeviceInfo(hDevInfo, i, byref(deviceInfoData)):
                err = GetLastError()
                if err == ERROR_NO_MORE_ITEMS:
                    break
                else:
                    raise WinError(err)
            i += 1
            hardwareId = WinUsb.GetDeviceHardwareId(hDevInfo, deviceInfoData)
            if hardwareId.startswith("USB\\ROOT_HUB"):
                continue
            driverInfoData.DriverVersion = 0
            SetupDiGetDeviceInstallParams(hDevInfo, byref(deviceInfoData),
                                          byref(deviceInstallParams))
            deviceInstallParams.FlagsEx |= DI_FLAGSEX_INSTALLEDDRIVER
            SetupDiSetDeviceInstallParams(hDevInfo, byref(deviceInfoData),
                                          byref(deviceInstallParams))
            SetupDiBuildDriverInfoList(hDevInfo, byref(deviceInfoData),
                                       SPDIT_COMPATDRIVER)
            if not SetupDiEnumDriverInfo(hDevInfo, byref(deviceInfoData),
                                         SPDIT_COMPATDRIVER, 0,
                                         byref(driverInfoData)):
                err = GetLastError()
                if err == ERROR_NO_MORE_ITEMS:
                    devices[hardwareId] = DeviceInfo(
                        name="<unknown name>",
                        version="",
                        hardwareId=hardwareId,
                        provider="<unknown provider",
                    )
                    continue
                else:
                    raise WinError(err)
            version = driverInfoData.DriverVersion
            versionStr = "%d.%d.%d.%d" % (
                (version >> 48) & 0xFFFF, (version >> 32) & 0xFFFF,
                (version >> 16) & 0xFFFF, version & 0xFFFF)
            devices[hardwareId] = DeviceInfo(
                driverInfoData.Description,
                versionStr,
                hardwareId,
                driverInfoData.ProviderName,
            )
        return devices
示例#2
0
    def ListDevices():
        devices = {}
        guid = GUID()
        CLSIDFromString("{A5DCBF10-6530-11D2-901F-00C04FB951ED}", byref(guid))
        hDevInfo = SetupDiGetClassDevs(
            guid,
            "USB",  # Enumerator
            0,
            DIGCF_PRESENT | DIGCF_ALLCLASSES
        )
        if hDevInfo == INVALID_HANDLE_VALUE:
            raise WinError()
        deviceInfoData = SP_DEVINFO_DATA()
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA)
        driverInfoData = SP_DRVINFO_DATA()
        driverInfoData.cbSize = sizeof(SP_DRVINFO_DATA)
        deviceInstallParams = SP_DEVINSTALL_PARAMS()
        deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS)

        i = 0
        while True:
            if not SetupDiEnumDeviceInfo(hDevInfo, i, byref(deviceInfoData)):
                err = GetLastError()
                if err == ERROR_NO_MORE_ITEMS:
                    break
                else:
                    raise WinError(err)
            i += 1
            hardwareId = WinUsb.GetDeviceHardwareId(hDevInfo, deviceInfoData)
            if hardwareId.startswith("USB\\ROOT_HUB"):
                continue
            driverInfoData.DriverVersion = 0
            SetupDiGetDeviceInstallParams(
                hDevInfo,
                byref(deviceInfoData),
                byref(deviceInstallParams)
            )
            deviceInstallParams.FlagsEx |= DI_FLAGSEX_INSTALLEDDRIVER
            SetupDiSetDeviceInstallParams(
                hDevInfo,
                byref(deviceInfoData),
                byref(deviceInstallParams)
            )
            SetupDiBuildDriverInfoList(
                hDevInfo,
                byref(deviceInfoData),
                SPDIT_COMPATDRIVER
            )
            if not SetupDiEnumDriverInfo(
                hDevInfo,
                byref(deviceInfoData),
                SPDIT_COMPATDRIVER,
                0,
                byref(driverInfoData)
            ):
                err = GetLastError()
                if err == ERROR_NO_MORE_ITEMS:
                    devices[hardwareId] = DeviceInfo(
                        name = "<unknown name>",
                        version = "",
                        hardwareId = hardwareId,
                        provider = "<unknown provider",
                    )
                    continue
                else:
                    raise WinError(err)
            version = driverInfoData.DriverVersion
            versionStr = "%d.%d.%d.%d" % (
                (version >> 48) & 0xFFFF,
                (version >> 32) & 0xFFFF,
                (version >> 16) & 0xFFFF,
                version & 0xFFFF
            )
            devices[hardwareId] = DeviceInfo(
                driverInfoData.Description,
                versionStr,
                hardwareId,
                driverInfoData.ProviderName,
            )
        return devices