回到家里这个WE我遇到了一个关于蒸汽视频游戏的问题:我经常滞后,通过搜索网络我发现这来自一些控制设备,就像我的WE键盘一样。
解决方案是使用标准右键单击=>停用设备管理器(人机接口设备部分)中的某些HID设备。禁用操作。所以我开始编写一个小实用程序来启动游戏时禁用这些设备,并在退出后重新启用它们。
使用SetupDI API函数,我设法隔离了我想要禁用的设备,但是当我通过应用DICS_DISABLE操作禁用它们时,而不是像我用鼠标右键方法禁用它们一样,设备变为&# 34;未知设备"在设备管理器中。我必须更新设备的驱动程序才能将它们恢复到设备管理器中。我也尝试过DICS_STOP操作,但是这个设备只是从DM中消失了......
此操作中是否有我缺少的东西?
这是我的原型代码: (console application,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;
}