|
发表于 2011-7-29 10:39:09
|
显示全部楼层
粗略代码请看下面:#include <TreeViewConstants.au3>
#include <GUITreeView.au3>
#include <GuiToolBar.au3>
#include <SetupApi.au3>
Local $TitleInDM,$sDeviceId, $hDevs, $tDevInfo
$TitleInDM = "HID Keyboard Device";这里如果有两个device名字是一样的,只返回第一个deviceID
$sDeviceId = _CM_Get_Device_ID_By_Name_Ex($TitleInDM)
If $sDeviceId == "" Then Exit
; Obtain a handle to device information set.
_SetupDiCreateDeviceDevs($sDeviceId, $hDevs, $tDevInfo)
; The following 4 calls always returns True if succeeds without an error.
_SetupDiDisableDevice($hDevs, $tDevInfo, 1) ; Disable
Sleep(5000)
_SetupDiDisableDevice($hDevs, $tDevInfo, 0) ; Enable
Sleep(5000)
_SetupDiRemoveDevice($hDevs, $tDevInfo, 1) ; Forcibly remove this device
Sleep(5000)
_CM_Scan_Device_Changes("") ; Detect the device that has been removed from your system.
;Release handle and vars.
_SetupDiDestroyDeviceInfoList($hDevs)
_SetupDiApiBufferFree($tDevInfo)
; #### FUNCTION ####
; ================================================================================
; Name : _CM_Get_Device_ID_By_Name_Ex
; Description : Specifies a device friendly name (or device description), retrieves the device instance ID binds to this name.
; Parameter(s) : $sFriendlyName - Specifies a device friendly name, or description.
; : $fMatchAll - A value of TRUE indicates the full words is matched, default to TRUE.
; : $sDeviceID - Device identifier string of the device from which the match is started, can be NULL.
; : $hMachine - Supplies a machine handle, on which the function to execute, default to local.
; Return values : If a device instance is matched, the return value is set to the device instance identifier, otherwise returns NULL.
; Author : Pusofalse
; ================================================================================
Func _CM_Get_Device_ID_By_Name_Ex($sFriendlyName, $fMatchAll = True, $sDeviceID = "", $hMachine = 0)
Local $aChild, $sDescr, $sName, $hDevInst, $sVal
If IsString($sDeviceID) = 0 Then
$hDevInst = $sDeviceID
Else
$hDevInst = _CM_Locate_DevNode_Ex($sDeviceID, $hMachine)
EndIf
$aChild = _CM_Enumerate_Children_Ex($hDevInst, $hMachine)
For $i = 1 To $aChild[0]
$hDevInst = _CM_Locate_DevNode_Ex($aChild[$i], $hMachine)
$sDescr = _CM_Get_DevNode_Registry_Property_Ex($hDevInst, 1, $hMachine)
$sName = _CM_Get_DevNode_Registry_Property_Ex($hDevInst, 0xD, $hMachine)
If $sName <> "" Then $sDescr = $sName
If $sDescr = "" Then $sDescr = "[Unknown device]"
If $fMatchAll Then
If $sDescr = $sFriendlyName Then Return $aChild[$i];我把这里换成msgbox发现可以显示两个deviceID,但是不懂为什么只返回一个?
Else
If StringInStr($sDescr, $sFriendlyName) Then Return $aChild[$i]
EndIf
$sVal = _CM_Get_Device_ID_By_Name_Ex($sFriendlyName, $fMatchAll, $hDevInst, $hMachine)
If $sVal <> "" Then Return $sVal;把这里换成msgbox发现这个递归会调用很多次
Next
Return ""
EndFunc ;==>_CM_Get_Device_ID_By_Name_Ex |
|