Khi trở về nhà, chúng tôi gặp vấn đề liên quan đến trò chơi video hơi nước: Tôi nhận được sự chậm trễ thường xuyên, và bằng cách tìm kiếm trên web tôi thấy rằng điều này đến từ một số thiết bị điều khiển bàn phím WE của tôi.Cố gắng vô hiệu hóa một thiết bị trong môi trường cửa sổ
Giải pháp là tắt một số thiết bị HID trong trình quản lý thiết bị (phần thiết bị giao diện con người) bằng thao tác nhấp chuột phải => tắt thao tác. Vì vậy, tôi bắt đầu viết mã một tiện ích nhỏ để vô hiệu hóa các thiết bị này trong khi khởi chạy trò chơi và kích hoạt lại chúng sau khi thoát.
Sử dụng chức năng API SetupDI, tôi quản lý để cô lập các thiết bị tôi muốn vô hiệu hóa, nhưng khi tôi vô hiệu hóa chúng bằng cách áp dụng thao tác DICS_DISABLE, thay vì hoạt động như tôi đã tắt chúng bằng phương pháp nút chuột phải, thiết bị sẽ trở thành " thiết bị không xác định "trong trình quản lý thiết bị. Tôi phải cập nhật trình điều khiển của thiết bị để đưa họ trở lại trình quản lý thiết bị. Tôi cũng đã thử các hoạt động DICS_STOP, nhưng với một trong những thiết bị này chỉ đơn giản biến mất khỏi DM ...
Có điều gì đó tôi thiếu trong hoạt động này? Đây là mã mẫu thử nghiệm của tôi: (giao diện điều khiển ứng dụng, x64) => hệ thống là x64, và nếu ứng dụng của tôi là 32 bit, tất cả các thao tác thiết bị đơn giản là không thành công.
#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;
}
bạn đã yêu cầu khởi động lại sau khi thực thi mã này vì tôi đang triển khai cùng trạng thái mã này cho các thiết bị thay đổi DM cho tôi nhưng yêu cầu tôi khởi động lại hệ thống – bhupinder