使用ctypes访问USB设备信息?

2024-09-23 10:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python和ctypes以某种方式访问连接到PC的USB设备的信息。这可以从.dll实现吗?我试着找到它的安装位置,它的供应商等等

例如:

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

但是我怎样才能找到正确的.dll呢?我搜索了一下,但似乎什么也没有。在


Tags: import信息方式ctypes供应商atusbdll
1条回答
网友
1楼 · 发布于 2024-09-23 10:28:51

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

我使用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

然后简单地运行:

^{pr2}$

相关问题 更多 >