【问题标题】:Access USB device info with ctypes?使用 ctypes 访问 USB 设备信息?
【发布时间】:2014-12-30 05:51:00
【问题描述】:

我正在使用带有ctypes 的python 以某种方式访问​​有关连接到PC 的USB 设备的信息。这可以通过 .dll 实现吗?我试图找到它的安装位置、供应商等。

一个例子:

>>> import ctypes import windll
>>> windll.kernel32
<WindDLL 'kernel32', handle 77590000 at 581b70>

但是我如何找到正确的 .dll 呢?我用谷歌搜索,但似乎没有任何东西。

【问题讨论】:

标签: python python-3.x dll kernel ctypes


【解决方案1】:

最后我使用了一种更简单的方法。

我使用 Python 附带的 winreg 模块来访问 Windows 注册表。 HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices 跟踪所有已安装的设备(当前连接或未连接)。因此,我从那里获取所有设备信息并检查设备当前是否已连接,我只需 os.path.exists 设备的存储字母(即G:)。存储信可以从密钥MountedDevices获取。

例子:

# Make it work for Python2 and Python3
if sys.version_info[0]<3:
    from _winreg import *
else:
    from winreg import *

# Get DOS devices (connected or not)
def get_dos_devices():
    ddevs=[dev for dev in get_mounted_devices() if 'DosDevices' in dev[0]]
    return [(d[0], regbin2str(d[1])) for d in ddevs]

# Get all mounted devices (connected or not)
def get_mounted_devices():
    devs=[]
    mounts=OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\MountedDevices')
    for i in range(QueryInfoKey(mounts)[1]):
        devs+=[EnumValue(mounts, i)]
    return devs

# Decode registry binary to readable string
def regbin2str(bin):
    str=''
    for i in range(0, len(bin), 2):
        if bin[i]<128:
            str+=chr(bin[i])
    return str

然后简单地运行:

get_dos_devices()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2012-06-25
    • 2016-04-21
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多