wjscxs 发表于 2010-9-24 11:04:30

setupapi.au3 查看USB的使用状态

本帖最后由 wjscxs 于 2010-9-28 14:33 编辑


setupid.au3写的好多,真是难以看懂,看懂了又不理解有些概念.还是求P版指导下如何查看USB的使用状态。如图
如何查看USB连接了那些设备?

kingfirekkk 发表于 2010-9-24 17:00:10

啊哈,我也想知道答案,不过你的setupid.au3怎么不放上来....

redapple2008 发表于 2010-9-24 21:43:17

setupid.au3是什么文件?

rolaka 发表于 2010-9-24 23:24:09

setupid.au3是什么文件?
redapple2008 发表于 2010-9-24 21:43 http://www.autoitx.com/images/common/back.gif
本坛传说中的人物p神写的一个udf...

wjscxs 发表于 2010-9-25 07:41:30

#include <SetupAPI.au3>
#include<array.au3>
_disp("USB",False,"usb")
Func _disp($HDV,$STATS="",$stringN="")
Local $hDevs, $tDevInfo, $sDeviceID,$aMouse = [],$sDescr,$sName,$fResult,$hDevInst,$aChild
$hDevs = _SetupDiGetClassDevs($DIGCF_PRESENT, $HDV)
While _SetupDiEnumDeviceInfo($hDevs, $aMouse, $tDevInfo)
      $aMouse += 1
      $sDescr = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_DEVICEDESC)
      $sName = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_FRIENDLYNAME)
      If $sName <> "" Then $sDescr = $sName
      Redim $aMouse[$aMouse + 1]
      $aMouse[$aMouse] = $sDescr      ; 鼠标描述信息
      $aMouse[$aMouse] = _SetupDiGetDeviceInstanceID($hDevs, $tDevInfo) ; 设备范例ID
WEnd
; 枚举网卡只需把"Mouse"改为"Net",磁盘则对应DiskDrive,显卡对应Display。
_SetupDiDestroyDeviceInfoList($hDevs)
for $i=1 To $aMouse

ifStringLeft($aMouse[$i],3) = $stringN Then; 鼠标设备范例ID。
$hDevInst = _CM_Locate_DevNode($aMouse[$i])
$aChild = _CM_Enumerate_Children($hDevInst)
For $i=0 to $aChild
        If StringInStr(mouselist(),$aChild[$i]) Then
                ExitLoop               
        EndIf
        Next
$sDeviceID = $aMouse[$i]
EndIf
_SetupDiCreateDeviceDevs($sDeviceID, $hDevs, $tDevInfo)
$fResult = _SetupDiDisableDevice($hDevs, $tDevInfo, $STATS) ; 启用。
_SetupDiDestroyDeviceInfoList($hDevs) ; 销毁设备信息集。
; _CM_Disable_DevNode / _CM_Enable_DevNode 同样可以实现对硬件的禁用启用操作。
; 但_CM_Enable_DevNode不能启用设备管理器或_SetupDiDisableDevice禁用过的硬件。
Next
_ArrayDisplay($aMouse)
EndFunc

Func mouselist()
        Local $hDevs, $tDevInfo, $aMouse = []
$hDevs = _SetupDiGetClassDevs($DIGCF_PRESENT, "mouse")
While _SetupDiEnumDeviceInfo($hDevs, $aMouse, $tDevInfo)
      $aMouse += 1
      $sDescr = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_DEVICEDESC)
      $sName = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_FRIENDLYNAME)
      If $sName <> "" Then $sDescr = $sName
      Redim $aMouse[$aMouse + 1]
      $aMouse[$aMouse] = $sDescr      ; 鼠标描述信息
      $aMouse[$aMouse] = _SetupDiGetDeviceInstanceID($hDevs, $tDevInfo) ; 设备范例ID
        WEnd
        Return _ArrayToString($aMouse,"@",1,$aMouse)
        EndFunc

wjscxs 发表于 2010-9-25 07:50:54

#Include <String.au3>
#include <SetupAPI.au3>
#include<array.au3>
;Opt("MustDeclareVars", 1)
Func _disp($HDV,$STATS="",$stringN="")
Local $hDevs, $tDevInfo, $sDeviceID,$aMouse = [],$sDescr,$sName,$fResult,$hDevInst,$aChild,$acompare
$hDevs = _SetupDiGetClassDevs($DIGCF_PRESENT, $HDV)
While _SetupDiEnumDeviceInfo($hDevs, $aMouse, $tDevInfo)
      $aMouse += 1
      $sDescr = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_DEVICEDESC)
      $sName = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_FRIENDLYNAME)
      If $sName <> "" Then $sDescr = $sName
      Redim $aMouse[$aMouse + 1]
      $aMouse[$aMouse] = $sDescr      ; 鼠标描述信息
      $aMouse[$aMouse] = _SetupDiGetDeviceInstanceID($hDevs, $tDevInfo) ; 设备范例ID
WEnd
; 枚举网卡只需把"Mouse"改为"Net",磁盘则对应DiskDrive,显卡对应Display。
_SetupDiDestroyDeviceInfoList($hDevs)
for $i=1 To $aMouse

ifStringLeft($aMouse[$i],3) = $stringN Then; 鼠标设备范例ID。
$hDevInst = _CM_Locate_DevNode($aMouse[$i])
$aChild = _CM_Enumerate_Children($hDevInst)
;_ArrayDisplay($aChild)
For $j=0 to $aChild
        ;MsgBox(16,$aChild[$j],mouselist())
        $acompare=_StringBetween($aChild[$j],'\','\')
        if $acompare <>1 And $acompare <> 0 Then
                ;MsgBox(16,"have","\.\")
        For $k=0 to $acompare
                $aChild[$j]=$acompare[$k]
        If StringInStr(mouselist(),$aChild[$j]) Then
;MsgBox(1,'j='&$j,"有子进程")
If $STATS=True Then
ContinueLoop 3
EndIf
        EndIf
Next
EndIf
Next
MsgBox(16,'i='&$i&"锁定","$sDeviceID = $aMouse[$i]")
$sDeviceID = $aMouse[$i]
EndIf
If $HDV="mouse" Then
$sDeviceID = $aMouse[$i]
ElseIf $HDV="cdrom" Then
        $sDeviceID = $aMouse[$i]
ElseIF$HDV="NET" Then
       $sDeviceID = $aMouse[$i]
EndIf


;MsgBox(16,'i='&$i,$sDeviceID)
_SetupDiCreateDeviceDevs($sDeviceID, $hDevs, $tDevInfo)
$fResult = _SetupDiDisableDevice($hDevs, $tDevInfo, $STATS) ; 启用/锁定。
_SetupDiDestroyDeviceInfoList($hDevs) ; 销毁设备信息集。
; _CM_Disable_DevNode / _CM_Enable_DevNode 同样可以实现对硬件的禁用启用操作。
; 但_CM_Enable_DevNode不能启用设备管理器或_SetupDiDisableDevice禁用过的硬件。
Next
;_ArrayDisplay($aMouse)
EndFunc

Func mouselist()
        Local $hDevs, $tDevInfo, $aMouse = [],$aMouseID="|",$i,$sDescr,$sName
$hDevs = _SetupDiGetClassDevs($DIGCF_PRESENT, "mouse")
While _SetupDiEnumDeviceInfo($hDevs, $aMouse, $tDevInfo)
      $aMouse += 1
      $sDescr = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_DEVICEDESC)
      $sName = _SetupDiGetDeviceRegistryProperty($hDevs, $tDevInfo, $SPDRP_FRIENDLYNAME)
      If $sName <> "" Then $sDescr = $sName
      Redim $aMouse[$aMouse + 1]
      $aMouse[$aMouse] = $sDescr      ; 鼠标描述信息
      $aMouse[$aMouse] = _SetupDiGetDeviceInstanceID($hDevs, $tDevInfo) ; 设备范例ID
        WEnd
        for $i=0 to $aMouse
                $aMouseID=$aMouseID & $aMouse[$i] &'|'
        Next
        Return $aMouseID
        EndFunc

pusofalse 发表于 2010-9-25 12:59:28

setupid - -|||| 这是谁写的?

获取USB HUB连接的设备用_CM_Enumerate_Children。
#include <Array.au3>
#include <SetupAPI.au3>

$sDeviceId = "USB\ROOT_HUB\4&1301BE71&0"
$hDevInst = _CM_Locate_DevNode($sDeviceId)
$aAttached = _CM_Enumerate_Children($hDevInst)
_ArrayDisplay($aAttached, "Attached devices")

获取任何设备的子设备,都可以用以上方法。

$aAttached是一维数组,里面包含子设备的Device Instance Id,获取设备的显示名称,调用_CM_Get_Device_Display_Name_Ex。

在写FSeven_v4.7时,查了很多资料想获取每个设备的所需电源是多少毫安,可惜不会。
FSeven_v4.7中有获取HUB端口数量、判断供电方式、判断低速高速的方法,具体看其中的_UsbHubAdvanced和_UsbHubRefreshAttachedDevices函数。

FSeven_v4.7 设备管理器 - http://www.autoitx.com/forum.php?mod=viewthread&tid=14610&highlight=fsev

PS: 把标题更正过来吧,如果你所说的setupid是SetupApi的话。

hzxymkb 发表于 2010-9-25 14:17:06

P版一来!啥事都解决了!{:face (356):}
页: [1]
查看完整版本: setupapi.au3 查看USB的使用状态