/*----------------------以下为类的头文件声明 --------------------------*/
#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,
®Type,
(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,
®Type,
(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*)¶ms, 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,
®Type,
(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,
®Type,
(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*)¶ms, 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;
}
/*--------------------------结束---------------------------*/