通过这次 WE 回家,我遇到了一个关于 Steam 视频游戏的问题:我经常出现延迟,通过网络搜索,我发现这来自一些控制设备,例如我的 WE 键盘。
解决方案是在设备管理器(人机界面设备部分)中使用标准的右键单击 => 禁用操作停用某些 HID 设备。所以我开始编写一个小实用程序来在启动游戏时禁用这些设备,并在退出后重新启用它们。
使用 SetupDI API 函数,我设法隔离了我想禁用的设备,但是当我通过应用 DICS_DISABLE 操作禁用它们时,而不是像我用鼠标右键方法禁用它们一样,这些设备在设备管理器。我必须更新设备的驱动程序才能让它们回到设备管理器中。我也尝试了 DICS_STOP 操作,但是有了这个,设备就会从 DM 中消失......
在这个操作中我有什么遗漏吗?
这是我的原型代码:(控制台应用程序,x64)=> 系统是 x64,如果我的应用程序是 32 位,所有设备操作都会失败。
#include <stdio.h>
#include <Windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#pragma comment (lib, "Newdev.lib")
#pragma comment (lib, "Setupapi.lib")
int main(int argc, void * argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
SP_PROPCHANGE_PARAMS params; // params to set in order to enable/disable the device
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
LPTSTR servBuffer = NULL;
DWORD buffersize = 0;
DWORD servBufferSize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_SERVICE,
&DataT,
(PBYTE)servBuffer,
servBufferSize,
&servBufferSize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (servBuffer) LocalFree(servBuffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
servBuffer = (LPTSTR)LocalAlloc(LPTR,servBufferSize * 2);
}
else
{
// Insert error handling here.
break;
}
}
if (strstr((char *)buffer, "(HID)") && NULL == servBuffer)
{
printf("New device found : %s\n", buffer);
printf("disabling...\n");
// init the structure
params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.HwProfile = 0;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
params.StateChange = DICS_DISABLE;
// prepare operation
if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, ¶ms.ClassInstallHeader, sizeof(params)))
{
printf("Error while preparing params !\n");
break;
}
// launch op
if (!SetupDiCallClassInstaller(DICS_DISABLE, hDevInfo, &DeviceInfoData))
{
printf("Error while calling OP ! Return code is %x\n", GetLastError());
continue;
}
printf("done.\n\n");
}
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}