Windows平台C/C++实现对键盘或鼠标驱动设备的禁用、启动、卸载以及刷新设备管理列表功能

4 篇文章 1 订阅

/*----------------------以下为类的头文件声明 --------------------------*/
#ifndef KEYBOARDMOUSE_DEVICE_CONTROLLER_H
#define KEYBOARDMOUSE_DEVICE_CONTROLLER_H

#include <QString>
#include<windows.h>
#include<cfgmgr32.h>  

extern "C" {
#include "hidsdi.h"
#include <setupapi.h>
}

#pragma comment(lib,"setupapi.lib")
#pragma comment(lib,"hid.lib")
#pragma comment(lib,"Wtsapi32.lib")

class KeyboardMouseDeviceController
{

public:

    /**
        Description:
            构造函数
        @param    无
        @return    无
    */
    KeyboardMouseDeviceController();
    /**
        Description:
            析构函数
        @param    无
        @return    无
    */
    ~KeyboardMouseDeviceController();

    /**
        Description:
            该方法用于获取光源连接状态
        @param    无
        @return    无
    */
    DWORD ControlMouseDevice(bool bEnable);
    
    /**
        Description:
            该方法用于获取光源连接状态
        @param    无
        @return    无
    */
    DWORD ControlKeyboardDevice(bool bEnable);
private:

    /**
        Description:
            该方法用于获取光源连接状态
        @param    无
        @return    无
    */
    bool ScanForHardwareChange();   
};

#endif //KEYBOARDMOUSE_DEVICE_CONTROLLER_H
 

/*------------------------------以下为类的实现--------------------------*/
#include "KeyboardMouseDeviceController.h"
#pragma region 构造函数和析构函数
/************************************************************************/
/* 构造函数                                                             */
/************************************************************************/
KeyboardMouseDeviceController::KeyboardMouseDeviceController()
{


}

/************************************************************************/
/* 析构函数                                                             */
/************************************************************************/
KeyboardMouseDeviceController::~KeyboardMouseDeviceController()
{

}
#pragma endregion


/************************************************************************/
/* 刷新设备驱动列表                                                   */
/************************************************************************/
bool KeyboardMouseDeviceController::ScanForHardwareChange()
{
    DEVINST   devInst;
    CONFIGRET   status;

    //得到设备管理树的根结点   
    status = CM_Locate_DevNode(&devInst, NULL, CM_LOCATE_DEVNODE_NORMAL);
    if (status != CR_SUCCESS)
    {
        printf("CM_Locate_DevNode   failed:   %x\n", status);
        return   false;
    }
    //刷新   
    status = CM_Reenumerate_DevNode(devInst, CM_REENUMERATE_NORMAL);
    if (status != CR_SUCCESS)
    {
        printf("CM_Reenumerate_DevNode   failed:   %x\n", status);
        return false;
    }
    return true;
}

/************************************************************************/
/* 键盘驱动设备控制                                                    */
/************************************************************************/
DWORD KeyboardMouseDeviceController::ControlKeyboardDevice(bool bEnable)
{
    
    if(bEnable)
    {
        if(!ScanForHardwareChange())
        {
            
            return -1;
            
        }
        return 0;
    }
    
    
    GUID HidGuid;
    HANDLE hDevInfo;
    SP_DEVINFO_DATA    spdevInfoData;
    HidD_GetHidGuid(&HidGuid);
    hDevInfo = SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_PROFILE | DIGCF_DEVICEINTERFACE);
    if (INVALID_HANDLE_VALUE == hDevInfo)
    {
        return -1;
    }

    spdevInfoData.cbSize = sizeof(spdevInfoData);
    int idx;

    SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
    params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    params.Scope = DICS_FLAG_CONFIGSPECIFIC;
    if (bEnable)
    {
        params.StateChange = DICS_ENABLE;
    }
    else
    {
        params.StateChange = DICS_DISABLE;
    }
    params.HwProfile = 0;
    for (idx = 0; SetupDiEnumDeviceInfo(hDevInfo, idx, &spdevInfoData); idx++)
    {
        LPTSTR buffer = NULL;
        DWORD   buffersize = 0;
        //ULONG   len;
        //step1 get the buffer size 
        DWORD regType;
        while (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &spdevInfoData,
            SPDRP_HARDWAREID,
            &regType,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                // Change the buffer size.  
                if (buffer) LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
            }
            else
            {
                // Insert error handling here.  
                break;
            }
        }

        OutputDebugString(buffer);
        //step2 get the information      
        while (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &spdevInfoData,
            SPDRP_DEVICEDESC,
            &regType,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                // Change the buffer size.  
                if (buffer) LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
            }
            else
            {
                // Insert error handling here.  
                break;
            }
        }

        OutputDebugString(buffer);

        QString strDeviceRegistryProperty = QString::fromWCharArray(buffer);
        strDeviceRegistryProperty = strDeviceRegistryProperty.toUpper();//重置为大写
    
        if (strDeviceRegistryProperty.contains("KEYBOARD"))//MOUSE KEYBOARD BLUETOOTH AUDIO
        {
            printf("strDeviceRegistryProperty =%s ,idx=%d\n", strDeviceRegistryProperty.toLocal8Bit().data(), idx);

            SetupDiSetClassInstallParams(hDevInfo, &spdevInfoData, (SP_CLASSINSTALL_HEADER*)&params, sizeof(SP_PROPCHANGE_PARAMS));

            if (!bEnable)
            {
                if (!SetupDiRemoveDevice(hDevInfo, &spdevInfoData))
                {
                    DWORD dwerror = GetLastError();
                    printf("1 GetLastError() == %d\n", dwerror);
                }
            }
    
            /*if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spdevInfoData))
            {
                DWORD dwerror = GetLastError();
                printf("1 GetLastError() == %d\n", dwerror);
            }*/
        }
    }

    if (!SetupDiDestroyDeviceInfoList(hDevInfo))
    {
        DWORD dwerror = GetLastError();
        printf("2 GetLastError() == %d\n", dwerror);
    }

    return 0;
}

/************************************************************************/
/* 鼠标驱动设备控制                                                     */
/************************************************************************/
DWORD KeyboardMouseDeviceController::ControlMouseDevice(bool bEnable)
{
    GUID HidGuid;
    HANDLE hDevInfo;
    SP_DEVINFO_DATA    spdevInfoData;
    HidD_GetHidGuid(&HidGuid);
    hDevInfo = SetupDiGetClassDevs(&HidGuid, NULL, NULL, DIGCF_PROFILE | DIGCF_DEVICEINTERFACE);
    if (INVALID_HANDLE_VALUE == hDevInfo)
    {
        return -1;
    }

    spdevInfoData.cbSize = sizeof(spdevInfoData);
    int idx;

    SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
    params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    params.Scope = DICS_FLAG_CONFIGSPECIFIC;
    if (bEnable)
    {
        params.StateChange = DICS_ENABLE;
    }
    else
    {
        params.StateChange = DICS_DISABLE;
    }
    params.HwProfile = 0;
    for (idx = 0; SetupDiEnumDeviceInfo(hDevInfo, idx, &spdevInfoData);idx++)
    {
        LPTSTR buffer = NULL;
        DWORD   buffersize = 0;
        //ULONG   len;
        //step1 get the buffer size 
        DWORD regType;
        while (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &spdevInfoData,
            SPDRP_HARDWAREID,
            &regType,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                // Change the buffer size.  
                if (buffer) LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
            }
            else
            {
                // Insert error handling here.  
                break;
            }
        }

        OutputDebugString(buffer);
        //step2 get the information      
        while (!SetupDiGetDeviceRegistryProperty(
            hDevInfo,
            &spdevInfoData,
            SPDRP_DEVICEDESC,
            &regType,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                // Change the buffer size.  
                if (buffer) LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
            }
            else
            {
                // Insert error handling here.  
                break;
            }
        }

        OutputDebugString(buffer);

        //CString strDeviceRegistryProperty(buffer);
        //strDeviceRegistryProperty.MakeUpper();
    
        QString strDeviceRegistryProperty = QString::fromWCharArray(buffer);
        strDeviceRegistryProperty = strDeviceRegistryProperty.toUpper();//重置为大写

        //if (strDeviceRegistryProperty.Find(_T("MOUSE")) != -1)
        if (strDeviceRegistryProperty.contains("MOUSE"))
        {
            printf("strDeviceRegistryProperty =%s ,idx=%d\n", strDeviceRegistryProperty.toLocal8Bit().data(), idx);
            SetupDiSetClassInstallParams(hDevInfo, &spdevInfoData, (SP_CLASSINSTALL_HEADER*)&params, sizeof(SP_PROPCHANGE_PARAMS));

            BOOL bret = SetupDiChangeState(hDevInfo, &spdevInfoData);
            if (!bret)
            {
                DWORD dwerror = GetLastError();
                printf("1 GetLastError() == %d\n", dwerror);
            }
        }
    }

    if (!SetupDiDestroyDeviceInfoList(hDevInfo))
    {
        DWORD dwerror = GetLastError();
        printf("2 GetLastError() == %d\n", dwerror);
    }

    return 0;
}

/*-----------------------------以下为调用实例 ---------------------------------*/
#include "KeyboardMouseDeviceController.h"
int main(int argc, char *argv[])
{
    KeyboardMouseDeviceController keyboard_mouse_device;
    //禁用鼠标和键盘
    keyboard_mouse_device.ControlMouseDevice(false);
    keyboard_mouse_device.ControlKeyboardDevice(false);
    
    //主进程忙碌100s
    for(int i=0;i<10;i++)
    {
        Sleep(10000);
    }
    
    //启用鼠标和键盘
    keyboard_mouse_device.ControlMouseDevice(true);
    keyboard_mouse_device.ControlKeyboardDevice(true);
    
    return 0;
}
/*--------------------------结束---------------------------*/

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值