如何以编程方式禁用"增强型电源管理"对于某些USB设备?

时间:2014-04-17 00:28:24

标签: windows usb registry

我正在处理与自定义USB设备交互的软件。设备将自身表示为HID设备,软件通过文件I / O与其进行交互。

由于Windows 8.1中的更改​​,操作系统不断重启设备,这导致软件出现问题。

根据知识库文章:http://support.microsoft.com/kb/2900614,Microsoft建议在遇到此问题时禁用USB设备的增强型电源管理功能,并在手动执行此操作后问题确实消失。

现在,我想修改软件的安装程序,为所有设备禁用此设置,而不仅仅是针对特定设备实例。

有没有办法实现这一目标?是通过Windows API调用,还是通过影响特定ProductID / VendorID组合的所有实例的注册表设置?

E.g。我想修改以下所有实例:

  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_11AE&PID_07CE

包括安装程序执行后可能连接到系统的任何未来实例。

3 个答案:

答案 0 :(得分:3)

以下是我编写的示例脚本,用于禁用所有Datalogic品牌USB设备(供应商ID 0x05F9)的电源管理。您可以更改" VID_05F9&"在For Each循环中的条件向下,使其与您需要修改的键匹配。请注意,还会处理必要的UAC提升。

'Filename: USBPMFIX.VBS
'Author:   Matthew Mellon <mmellon@ecrs.com>
'Date:     2014-12-12
'Desc:     Disables enhanced power management on all Datalogic USB devices.
'License:  This source code is released into the public domain.    

'Checks if the script is running elevated (UAC)
function isElevated
  Set shell = CreateObject("WScript.Shell")
  Set whoami = shell.Exec("whoami /groups")
  Set whoamiOutput = whoami.StdOut
  strWhoamiOutput = whoamiOutput.ReadAll

  If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then 
    isElevated = True
  Else
      isElevated = False
  End If
end function

'Re-runs the process prompting for priv elevation on re-run
sub uacPrompt

  'Check if we need to run in C or W script
  interpreter = "wscript.exe"
  If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
    interpreter = "wscript.exe"
  else
    interpreter = "cscript.exe"
  end if

  'Start a new instance with an elevation prompt first
  Set shellApp = CreateObject("Shell.Application")
  shellApp.ShellExecute interpreter, Chr(34) & WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1

  'End the non-elevated instance
  WScript.Quit
end sub

if not isElevated Then uacPrompt

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")

strKeyPath = "SYSTEM\CurrentControlSet\Enum\USB"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys

For Each Subkey in arrSubKeys
  If Left(Subkey,9) = "VID_05F9&" And Left(Right(Subkey,6),5) = "&MI_0" Then
    strKeyPath = "SYSTEM\CurrentControlSet\Enum\USB\"+Subkey
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrInnerSubKeys
    For Each InnerSubkey in arrInnerSubKeys
      strFullKey = "SYSTEM\CurrentControlSet\Enum\USB\"+Subkey+"\"+InnerSubkey+"\Device Parameters"
      objReg.SetDWORDValue HKEY_LOCAL_MACHINE, strFullKey, "EnhancedPowerManagementEnabled", 0
    Next   
  End If
Next

答案 1 :(得分:0)

您应该能够使用Registry Functions来动态更改导致问题的键。我以前没有做过,但我认为您可以在设备的自定义INF中将其添加为安装步骤,以确保在每次安装新设备时都会发生这种情况。

我不知道您是否可以控制设备固件,但您可以更改描述符,以便它预先告诉操作系统它不支持电源管理,如挂起或远程唤醒。

答案 2 :(得分:0)

另一种选择是将MS OS Descriptor添加到设备固件。您可以通过该描述符修改注册表属性。 所有必要的信息都可以在spec中找到。这是这样的描述符的样子。

const uint8_t msOs20DescriptorSet[10 + 76 + 6] =
{
    //
    // Microsoft OS 2.0 Descriptor Set Header
    //
    0x0A, 0x00,             // wLength - 10 bytes
    0x00, 0x00,             // wDescriptorType, MSOS20_SET_HEADER_DESCRIPTOR
    0x00, 0x00, 0x03, 0x06, // dwWindowsVersion – 0x06030000 for Windows Blue
    0x5C, 0x00,             // wTotalLength – 92 bytes

    //
    // Microsoft OS 2.0 Registry Value Feature Descriptor
    //
    0x4C, 0x00, // wLength - 76 bytes
    0x04, 0x00, // wDescriptorType – 4 for Registry Property
    0x04, 0x00, // wPropertyDataType - 4 for REG_DWORD
    0x3E, 0x00, // wPropertyNameLength – 62 bytes
    'E', 0, // Property Name - EnhancedPowerManagementEnabled
    'n', 0,
    'h', 0,
    'a', 0,
    'n', 0,
    'c', 0,
    'e', 0,
    'd', 0,
    'P', 0,
    'o', 0,
    'w', 0,
    'e', 0,
    'r', 0,
    'M', 0,
    'a', 0,
    'n', 0,
    'a', 0,
    'g', 0,
    'e', 0,
    'm', 0,
    'e', 0,
    'n', 0,
    't', 0,
    'E', 0,
    'n', 0,
    'a', 0,
    'b', 0,
    'l', 0,
    'e', 0,
    'd', 0,
    0, 0,
    0x04, 0x00,             // wPropertyDataLength – 4 bytes
    0x00, 0x00, 0x00, 0x00,  // PropertyData - 0x00000000

    // Microsoft OS 2.0 vendor revision descriptor
    0x06, 0x00, // wLength - 6 bytes
    0x08, 0x00, // wDescriptorType, MS_OS_20_FEATURE_VENDOR_REVISION
    0x01, 0x00, /* VendorRevision, if this value changes between enumerations the registry 
    property descriptors will be updated in registry during that enumeration. */
};