Attempting to disconnect a device in a Windows environment

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; // 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, &params.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;
}
+5
source share
2 answers

You have the wrong parameter, you need

SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData)

SP_PROPCHANGE_PARAMS DeviceInfoData.

. , . ( -, )

+8

devcon! this: devcon disable

0

All Articles