2014-11-03 84 views
0

我使用python與​​以某種方式訪問​​有關連接到PC的USB設備的信息。這可以通過.dll實現嗎?我嘗試找東西等,其中它的安裝,它的供應商等使用ctypes訪問USB設備信息?

一個例子:

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

可是我怎麼覺得這.dll文件是正確的?我搜索了一下,但似乎沒有任何東西。

+0

Winusb.ddl:http://msdn.microsoft.com/en-us/library/windows/hardware/ff540046%28v = vs.85%29.aspx也相關:http://stackoverflow.com/questions/12926923/winusb-dll-and-python-specifically-winusb-initialize – jmunsch 2014-11-03 17:16:47

回答

0

最後我用了一個更簡單的方法。

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