コード例 #1
0
    def GetDevicePaths():
        classGuid = GUID()
        CLSIDFromString(DRIVER_CLASS_GUID, byref(classGuid))
        hDevInfo = SetupDiGetClassDevs(
            classGuid, None, None, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
        )
        if hDevInfo == INVALID_HANDLE_VALUE:
            raise WinError()

        deviceInterfaceData = SP_DEVICE_INTERFACE_DATA()
        deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA)
        deviceInfoData = SP_DEVINFO_DATA()
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA)
        memberIndex = 0
        result = {}
        while True:
            if not SetupDiEnumDeviceInterfaces(
                hDevInfo,
                None,
                classGuid,
                memberIndex,
                byref(deviceInterfaceData)
            ):
                err = GetLastError()
                if err == ERROR_NO_MORE_ITEMS:
                    break
                else:
                    raise WinError(err)
            requiredSize = DWORD()
            SetupDiGetDeviceInterfaceDetail(
                hDevInfo,
                byref(deviceInterfaceData),
                None,
                0,
                byref(requiredSize),
                byref(deviceInfoData)
            )
            buf = create_string_buffer(requiredSize.value)
            pDiDetailData = cast(buf, PSP_DEVICE_INTERFACE_DETAIL_DATA)
            pDiDetailData.contents.cbSize = sizeof(
                SP_DEVICE_INTERFACE_DETAIL_DATA
            )
            SetupDiGetDeviceInterfaceDetail(
                hDevInfo,
                byref(deviceInterfaceData),
                pDiDetailData,
                requiredSize.value,
                byref(requiredSize),
                None
            )

            devicePath = wstring_at(addressof(pDiDetailData.contents) + 4)
            hardwareId = WinUsb.GetDeviceHardwareId(hDevInfo, deviceInfoData)
            result[hardwareId] = devicePath
            memberIndex += 1
        return result
コード例 #2
0
ファイル: WinUsb.py プロジェクト: nobusugi246/EventGhost
    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