首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式禁用某些USB设备的“增强电源管理”?

如何以编程方式禁用某些USB设备的“增强电源管理”?
EN

Stack Overflow用户
提问于 2014-04-17 08:28:24
回答 3查看 5.1K关注 0票数 2

我正在开发一款可以与自定义USB设备交互的软件。设备本身显示为HID设备,软件通过文件I/O与其交互。

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

根据此知识文库文章:http://support.microsoft.com/kb/2900614,微软建议禁用该设备的增强电源管理功能,如果该设备存在此问题,并且在手动执行此操作后该问题确实消失。

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

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

例如,我想修改下面的所有实例:

代码语言:javascript
复制
  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_11AE&PID_07CE

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

EN

回答 3

Stack Overflow用户

发布于 2015-02-18 06:08:34

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

代码语言:javascript
复制
'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
票数 3
EN

Stack Overflow用户

发布于 2014-04-19 03:13:41

您应该能够使用Registry Functions遍历并动态更改导致问题的密钥。我以前没有这样做过,但我认为您可以将此作为安装步骤添加到您的设备的自定义INF中,以确保每次安装新设备时都会发生此操作。

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

票数 0
EN

Stack Overflow用户

发布于 2020-03-06 17:21:40

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

代码语言:javascript
复制
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. */
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23122549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档