vb或c语言 如何屏蔽一个鼠标
现在电脑上有一个ps/2的鼠标和两个usb鼠标怎么才能完全屏蔽USB的鼠标,但保留ps2的鼠标,并且可以随时恢复呢?...
现在电脑上有一个ps/2的鼠标和两个usb鼠标 怎么才能完全屏蔽USB的鼠标,但保留ps2的鼠标,并且可以随时恢复呢?
展开
1个回答
展开全部
// 这是一个禁用USB接口的例子
// newUsbInfo.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <iostream>
#include <initguid.h>
#include <windows.h>
#include <setupapi.h>
#include <usbiodef.h>
#include <Usbioctl.h>
#include <cfgmgr32.h>
#include <cstring>
#include <stdlib.h>
#include <malloc.h>
#include <basetyps.h>
#include <tchar.h>
#include <SDKDDKVer.h>
#include <winioctl.h>
#pragma comment(lib,"setupapi")
using namespace std;
BOOL StateChange( DWORD dwNewState, DWORD dwDevID, HDEVINFO hDevInfo);
BOOL IsDisableable(DWORD dwDevID, HDEVINFO hDevInfo);
BOOL IsDisabled(DWORD dwDevID, HDEVINFO hDevInfo);
BOOL GetRegistryProperty( HDEVINFO DeviceInfoSet,
PSP_DEVINFO_DATA DeviceInfoData,
ULONG Property,
PVOID Buffer,
PULONG Length )
{
while ( !SetupDiGetDeviceRegistryProperty( DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
(BYTE *)*(TCHAR **)Buffer,
*Length,
Length))
{
// 长度不够则重新分配缓冲区
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
if (*(LPTSTR *)Buffer)
LocalFree(*(LPTSTR *)Buffer);
*(LPTSTR *)Buffer = reinterpret_cast<LPTSTR>((PCHAR)LocalAlloc(LPTR,*Length));
}
else
{
return false;
}
}
return (BOOL)(*(LPTSTR *)Buffer)[0];
}
BOOL EnableDevice(DWORD dwDevID, HDEVINFO hDevInfo)
{
return StateChange(DICS_ENABLE,dwDevID,hDevInfo);
}
BOOL DisableDevice(DWORD dwDevID, HDEVINFO hDevInfo)
{
return StateChange(DICS_DISABLE,dwDevID,hDevInfo);
}
BOOL ControlDevice(DWORD dwDevID, HDEVINFO hDevInfo , BOOL bEnable)
{
BOOL bCanDisable;
bCanDisable = (IsDisableable(dwDevID,hDevInfo) && (!IsDisabled(dwDevID,hDevInfo)));
cout << bEnable << bCanDisable <<endl;
if (bEnable)//Enable NetCard
{
if (!bCanDisable)
{
return EnableDevice(dwDevID,hDevInfo);
}
}
else //Disable NetCard
{
if (bCanDisable )
{
return DisableDevice(dwDevID,hDevInfo);
}
}
return TRUE;
}
BOOL SetCOMDevState(BOOL bEnable)
{
DWORD Status, Problem;
LPTSTR Buffer = NULL;
DWORD BufSize = 0;
BOOL bResult = FALSE;
// 返回所有设备信息
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES);
if (INVALID_HANDLE_VALUE == hDevInfo )
return bResult;
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
//////////////////////////////////////////////////////////////////////////
// 枚举设备
//////////////////////////////////////////////////////////////////////////
for ( DWORD DeviceId=0;
SetupDiEnumDeviceInfo( hDevInfo,DeviceId,&DeviceInfoData);
DeviceId++)
{
// 获得设备的状态
if (CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst ,0) != CR_SUCCESS)
continue;
// 获取设备类名
TCHAR szDevName [MAX_PATH] = _T("") ;
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS , &Buffer, (PULONG)&BufSize))
{
lstrcpyn( szDevName, Buffer, MAX_PATH );
}
//printf("%S \n",szDevName);
// if ( lstrcmp( szDevName, _T("Ports") ) == 0 )
if ( lstrcmp( szDevName, _T("USB") ) == 0 )
{
TCHAR szName [MAX_PATH] = _T("") ;
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize))
{
lstrcpyn( szName, Buffer, MAX_PATH );
}
printf("-------%S \n",szName);
if ( lstrcmp( szName, _T("USB") ) == 0 )
{
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER , &Buffer, (PULONG)&BufSize))
{
lstrcpyn( szName, Buffer, MAX_PATH );
// 获取设备描述
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC , &Buffer, (PULONG)&BufSize))
{
lstrcpyn( szName, Buffer, MAX_PATH );
printf("----------------%S \n",szName);
//if ( lstrcmp( szName, _T("USB Composite Device") ) == 0 )//找到设备
//{
cout << "begin->"<<endl;
bResult = ControlDevice(DeviceId,hDevInfo,bEnable);
//}
}
}
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return bResult;
}
BOOL StateChange( DWORD dwNewState, DWORD dwDevID, HDEVINFO hDevInfo)
{
SP_PROPCHANGE_PARAMS PropChangeParams;
SP_DEVINFO_DATA DevInfoData = {sizeof(SP_DEVINFO_DATA)};
SP_DEVINSTALL_PARAMS devParams;
//查询设备信息
if (!SetupDiEnumDeviceInfo( hDevInfo, dwDevID, &DevInfoData))
{
cout << "SetupDiEnumDeviceInfo FAILED" << endl;
return FALSE;
}
//设置设备属性变化参数
PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL; //使修改的属性保存在所有的硬件属性文件
PropChangeParams.StateChange = dwNewState;
PropChangeParams.HwProfile = 0;
//改变设备属性
if (!SetupDiSetClassInstallParams( hDevInfo,
&DevInfoData,
(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
{
cout << "SetupDiSetClassInstallParams FAILED" << endl;
return FALSE;
}
PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_CONFIGSPECIFIC;//使修改的属性保存在指定的属性文件
PropChangeParams.StateChange = dwNewState;
PropChangeParams.HwProfile = 0;
//改变设备属性并调用安装服务
if (!SetupDiSetClassInstallParams( hDevInfo,
&DevInfoData,
(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)) ||
!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DevInfoData))
{
cout << "failed" << GetLastError() << endl;
return TRUE;
}
else
{
cout << "success" << endl;
//判断是否需要重新启动
devParams.cbSize = sizeof(devParams);
if (!SetupDiGetDeviceInstallParams( hDevInfo, &DevInfoData, &devParams))
{
cout << "SetupDiGetDeviceInstallParams FAILED" << endl;
return FALSE;
}
if (devParams.Flags & (DI_NEEDRESTART|DI_NEEDREBOOT))
{
cout << "Need Restart Computer" << endl;
return TRUE;
}
return TRUE;
}
}
BOOL IsDisableable(DWORD dwDevID, HDEVINFO hDevInfo)
{
SP_DEVINFO_DATA DevInfoData = {sizeof(SP_DEVINFO_DATA)};
DWORD dwDevStatus,dwProblem;
if(!SetupDiEnumDeviceInfo(hDevInfo,dwDevID,&DevInfoData))
{
cout << "SetupDiEnumDeviceInfo FAILED" << endl;
return FALSE;
}
//查询设备状态
if(CM_Get_DevNode_Status(&dwDevStatus,&dwProblem,DevInfoData.DevInst,0)!=CR_SUCCESS)
{
cout << "CM_GET_DevNode_Status FAILED" << endl;
return FALSE;
}
return ((dwDevStatus & DN_DISABLEABLE) && (dwProblem != CM_PROB_HARDWARE_DISABLED));
}
BOOL IsDisabled(DWORD dwDevID, HDEVINFO hDevInfo)
{
SP_DEVINFO_DATA DevInfoData = {sizeof(SP_DEVINFO_DATA)};
DWORD dwDevStatus,dwProblem;
if(!SetupDiEnumDeviceInfo(hDevInfo,dwDevID,&DevInfoData))
{
cout << "SetupDiEnumDeviceInfo FAILED" << endl;
return FALSE;
}
//查询设备状态
if(CM_Get_DevNode_Status(&dwDevStatus,&dwProblem,DevInfoData.DevInst,0)!=CR_SUCCESS)
{
cout << "CM_GET_DevNode_Status FAILED" << endl;
return FALSE;
}
return ((dwDevStatus & DN_HAS_PROBLEM) && (dwProblem == CM_PROB_DISABLED));
}
int _tmain(int argc, _TCHAR* argv[])
{
SetCOMDevState(false);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询