Having returned home, we came to a problem regarding a steam video game: I received regular lags, and, searching the Internet, I found that this happened with some control devices, like my WE-keyboard.
The solution is to deactivate some HID devices in the device manager (device section of the user interface) with the standard operation of disabling right-click =>. So I started coding a small utility to turn off these devices when the game started and turn them on again after the release.
Using the API functions of SetupDI, I managed to isolate the devices that I wanted to disconnect, but when I disconnected them using the DICS_DISABLE operation, instead of acting as if I had disconnected them using the right mouse button method, the devices became โuninstalledโ "in device manager. I need to update device drivers to return them to device manager. I also tried the DICS_STOP operation, but with this device they just disappear from the DM ...
Is there something I am missing in this operation?
Here is my prototype code: (console application, x64) => x64 system, and if my application is 32 bits, all operations with the device simply fail.
#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;
hDevInfo = SetupDiGetClassDevs(NULL,
0,
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return 1;
}
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;
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
if (buffer) LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize * 2);
}
else
{
break;
}
}
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_SERVICE,
&DataT,
(PBYTE)servBuffer,
servBufferSize,
&servBufferSize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
if (servBuffer) LocalFree(servBuffer);
servBuffer = (LPTSTR)LocalAlloc(LPTR,servBufferSize * 2);
}
else
{
break;
}
}
if (strstr((char *)buffer, "(HID)") && NULL == servBuffer)
{
printf("New device found : %s\n", buffer);
printf("disabling...\n");
params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.HwProfile = 0;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
params.StateChange = DICS_DISABLE;
if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, ¶ms.ClassInstallHeader, sizeof(params)))
{
printf("Error while preparing params !\n");
break;
}
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 )
{
return 1;
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}