- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
使用设备管理器,用户可以明确启用/禁用设备,如下图所示。
对于给定的设备,我想知道它当前是否处于用户禁用/启用状态。
我尝试了以下方法
CM_Get_DevNode_Status(&status, &problem, data.DevInst, 0);
我希望 DN_STARTED
或 DN_DRIVER_LOADED
的存在会告诉我那。但是即使当操作系统正在加载/卸载驱动程序时,当设备连接/断开连接时,这些也可以为零。例如,已启用并加载了驱动程序的设备。 DN_STARTED
将为 1
,但当我们断开设备连接时,在设备条目从设备管理器中删除之前,它会设置为零。SetupDiGetDeviceRegistryProperty(..., SPDRP_INSTALL_STATE, ...)
我认为 CM_INSTALL_STATE_INSTALLED
状态应该意味着设备已启用。但即使对于禁用的设备,该函数也会返回此状态。使用 WMI 我能够获得所需的信息,但我在 PowerShell 中使用了 wmi。我不想使用 wmi,因为它很难在 native c++ 中实现。我使用了以下查询。
从 Win32_PnPEntity 中选择名称、可用性、ConfigManagerErrorCode、ConfigManagerUserConfig,其中名称 = 'NVIDIA Quadro M1000M'
上面查询中的ConfigManagerErrorCode,如果设置为22,表示设备已被禁用,21表示windows正在移除设备
我正在寻找非 wmi 解决方案。
最佳答案
可以从设备的问题代码中获取信息。我可以找到两种方法来获取它。
SetupDiGetDeviceProperty()
查询DEVPKEY_Device_ProblemCode
。CM_Get_DevNode_Status()
问题代码将出现在调用后的第二个参数中。问题代码 22 (CM_PROB_DISABLED
) 表示设备已被用户使用设备管理器或其他此类实用程序明确禁用。
示例代码
#include "stdafx.h"
#include <Windows.h>
#include <SetupAPI.h>
#include <Cfgmgr32.h>
#include <devguid.h>
#include <initguid.h>
#include "devpkey.h"
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
HDEVINFO hDevInfo = ::SetupDiGetClassDevs(&GUID_DEVCLASS_DISPLAY, NULL, NULL, 0); //only getting for GPUs on the machine
if (INVALID_HANDLE_VALUE != hDevInfo)
{
SP_DEVINFO_DATA data;
data.cbSize = (DWORD)sizeof(data);
for (unsigned int nIndex = 0; ::SetupDiEnumDeviceInfo(hDevInfo, nIndex, &data); nIndex++)
{
ULONG status = 0, problem = 0;
CONFIGRET cr = ::CM_Get_DevNode_Status(&status, &problem, data.DevInst, 0); //after the call 'problem' variable will have the problem code
if (CR_SUCCESS == cr)
{
cout << " problem " << problem <<endl;
if(problem == CM_PROB_DISABLED)
{ /*Do Something*/ }
DEVPROPTYPE propertyType;
const DWORD propertyBufferSize = 100;
BYTE propertyBuffer[propertyBufferSize];
std::fill(begin(propertyBuffer), end(propertyBuffer), BYTE(0));
DWORD requiredSize = 0;
if (SetupDiGetDeviceProperty(hDevInfo, &data, &DEVPKEY_Device_ProblemCode, &propertyType, propertyBuffer, propertyBufferSize, &requiredSize, 0)) //after the call 'propertyBuffer' will have error codes
{
unsigned long deviceProblemCode = *((unsigned long*)propertyBuffer);
cout << " deviceProblemCode " << deviceProblemCode << endl;
if(problem == CM_PROB_DISABLED)
{ /*Do Something*/ }
}
}
}
}
return 0;
}
示例输出
problem 0
deviceProblemCode 0
problem 22
deviceProblemCode 22
在问题中可以看到启用了 Intel(R) HD Graphics 530
,禁用了 NVIDIA Quadro M1000M
。因此在输出中我们得到了问题代码 0 和问题代码 22 (CM_PROB_DISABLED
)。
关于c++ - 如何知道设备是否已被用户明确禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47301350/
我在 linux 上工作。我对windows没有太多想法。 windows中文件的权限是如何组织的?我们在unix中是否有像chmod这样的api来更改权限? 最佳答案 对于 Windows,有一个名
应用程序编程接口(interface) (API) 是一组用于访问基于 Web 的软件应用程序的编程指令和标准。 如果出现 ,有人可以向我解释一下吗?谷歌地图 或 优酷 这是API哪个是softwar
我有两个应用程序,A 和 B,它们使用 android 库 C。B 有一个服务 A 想通过 C 使用,例如 在我的库中有一个类试图将它绑定(bind)到服务,
我正在正常或安全模式下启动相机应用程序,具体取决于使用我的应用程序执行的手势,但一旦用户选择应用程序并点击始终,则没有选项可以更改默认值,即使是从 Android 的设置菜单中也是如此. camera
我有一个数据集,本质上是一个稀疏二进制矩阵,表示两个集合的元素之间的关系。例如,让第一组是人(用他们的名字表示),例如像这样的东西: people = set(['john','jane','mike
何为pythonic? pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。 我的理解为,很+名词表达了一种特殊和强调的意味。
某些 Prolog 目标的确定性成功问题已经一次又一次地出现在 - 至少 - 以下问题: Reification of term equality/inequality Intersection an
我指的是 DateTime.TryParse(string s, out DateTime result) 重载,它尝试从字符串中解析 DateTime - 没有特定的格式正在指定。 我可以从http
2020 年 04 月 10 日,《中共中央国务院关于构建更加完善的要素市场化配置体制机制的意见》正式公布,将数据确立为五大生产要素(土地、资本、劳动力以及技术)之
有人可以解释一下 NSNotification 的 addObserver 函数中 notificationSender 的用途吗? 这是 Apple 文档的解释: notificationSende
我是一名优秀的程序员,十分优秀!