尝试在窗口环境中禁用设备

Trying to disable a device in the windows environment

本文关键字:环境 窗口      更新时间:2023-10-16

通过回到家里,这个我们遇到了一个关于蒸汽视频游戏的问题:我经常出现滞后,通过搜索网络,我发现这来自一些控制设备,作为我的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 : %sn", 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 %xn", GetLastError());
                continue;
            }
            printf("done.nn");
        }
        if (buffer) LocalFree(buffer);
    }

    if ( GetLastError()!=NO_ERROR &&
        GetLastError()!=ERROR_NO_MORE_ITEMS )
    {
        // Insert error handling here.
        return 1;
    }
    //  Cleanup
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return 0;
}

参数错误,您需要

SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData)

DeviceInfoData中填充SP_PROPCHANGE_PARAMS.

例如,请参阅此内容。(是中文的,只要看代码就够了)

使用过 devcon !试试这个:devcon 禁用