Const $DN_DISABLEABLE = 0x02000
Const $DICS_ENABLE = 0x0001
Const $DICS_DISABLE = 0x0002
Const $DICS_FLAG_GLOBAL = 0x0001
Const $DICS_FLAG_CONFIGSPECIFIC = 0x0002
Const $DIF_PROPERTYCHANGE = 0x0012
Const $tagSP_DEVINFO_DATA = "dword Size;byte Guid[16];dword DevInst;ulong_ptr Reserved"
Const $tagSP_CLASSINSTALL_HEADER = "dword Size;dword DIFCode"
Const $tagSP_PROPCHANGE_PARAMS = $tagSP_CLASSINSTALL_HEADER & ";dword State;dword Scope;dword HWProfile"
Local $sDevID = "PCI\VEN_10EC&DEV_8167&SUBSYS_E0001458&REV_10\4&BC67B8D&0&28F0"
If _DisableDevice($sDevID, True) Then ; True to disable, False to enable.
Msgbox(64, "Success", $sDevID & " : Success.")
Else
Msgbox(48, "Failure", $sDevID & " : Failure, error code: " & @error)
EndIf
Func _DisableDevice($sDevInstID, $fDisable = True)
Local $hDevs, $tDevInfo, $pDevInfo, $hDevInst, $iResult, $iDevNode, $tInstParam
$iResult = DllCall("Setupapi.dll", "ptr", "SetupDiCreateDeviceInfoList", "ptr", 0, "hWnd", 0)
If $iResult[0] = 0 Then Return SetError(__GetLastError(0), 0, 0)
$hDevs = $iResult[0]
$tDevInfo = DllStructCreate($tagSP_DEVINFO_DATA)
$pDevInfo = DllStructGetPtr($tDevInfo)
DllStructSetData($tDevInfo, "Size", DllStructGetSize($tDevInfo))
$iResult = DllCall("Setupapi.dll", "int", "SetupDiOpenDeviceInfo", "ptr", $hDevs, _
"str", $sDevInstID, "hWnd", 0, "dword", 0, "ptr", $pDevInfo)
If $iResult[0] = 0 Then Return SetError(__GetLastError($hDevs), 0, 0)
$iResult =DllCall("Setupapi.dll", "int", "SetupDiSetSelectedDevice" ,"ptr", $hDevs, "ptr", $pDevInfo)
If $iResult[0] = 0 Then Return SetError(__GetLastError($hDevs), 0, 0)
$hDevInst = DllStructGetData($tDevInfo, "DevInst")
$iResult = DllCall("Setupapi.dll", "long", "CM_Get_DevNode_Status", "ulong*", 0, _
"ulong*", 0, "dword", $hDevInst, "ulong", 0)
If $iResult[0] Then Return SetError($iResult[0], 0, 0 * __GetLastError($hDevs))
If ($fDisable = ($iResult[2] = 22)) Then Return SetError(85, 0, 1)
$iDevNode = $iResult[1]
If bitAND($iDevNode, $DN_DISABLEABLE) <> $DN_DISABLEABLE Then
Return SetError(50, 0, 0 * __GetLastError($hDevs))
EndIf
$tInstParam = DllStructCreate($tagSP_PROPCHANGE_PARAMS)
DllStructSetData($tInstParam, "Size", 8)
DllStructSetData($tInstParam, "DIFCode", $DIF_PROPERTYCHANGE)
DllStructSetData($tInstParam, "Scope", $DICS_FLAG_GLOBAL)
DllStructSetData($tInstParam, "HWProfile", 0)
DllStructSetData($tInstParam, "State", $DICS_DISABLE)
If $fDisable = False Then DllStructSetData($tInstParam, "State", $DICS_ENABLE)
$iResult = DllCall("Setupapi.dll", "int", "SetupDiSetClassInstallParams", "ptr", $hDevs, _
"ptr", $pDevInfo, "ptr", DllStructGetPtr($tInstParam), "dword", 20)
If $iResult[0] = 0 Then Return SetError(__GetLastError($hDevs), 0, 0)
$iResult = DllCall("Setupapi.dll", "int", "SetupDiChangeState", _
"ptr", $hDevs, "ptr", $pDevInfo)
If $iResult[0] = 0 Then Return SetError(__GetLastError($hDevs), 0, 0)
$tDevInfo = 0
$tInstParam = 0
Return SetError(__GetLastError($hDevs) * 0, 0, 1)
EndFunc ;==>_DisableDevice
Func __GetLastError($hDevs)
Local $iResult = DllCall("Kernel32.dll", "long", "GetLastError")
If $hDevs <> 0 Then DllCall("Setupapi.dll", "int", "SetupDiDestroyDeviceInfoList", "ptr", $hDevs)
Return $iResult[0]
EndFunc ;==>__GetLastError
以上代码适用于所有支持禁用操作的硬件设备。