禁用USB win32
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
#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; /* void test(){ BOOL rlt = FALSE; GUID devGUID = {0x36fc9e60L, 0xc465, 0x11cf, {0x80, 0x56, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}; HDEVINFO m_hDevInfo = INVALID_HANDLE_VALUE; SP_DEVINFO_DATA m_DeviceInfoData; RtlZeroMemory(&m_DeviceInfoData, sizeof(SP_DEVINFO_DATA)); // 初始化m_DeviceInfoData, m_DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); m_hDevInfo = SetupDiGetClassDevs(&devGUID,0,0,DIGCF_PRESENT ); for (DWORD i = 0; SetupDiEnumDeviceInfo ( m_hDevInfo, i, &m_DeviceInfoData ); i++ ) { DWORD DataT; LPTSTR buffer = NULL; DWORD buffersize = 0; while ( !SetupDiGetDeviceRegistryProperty ( m_hDevInfo, &m_DeviceInfoData, SPDRP_DEVICEDESC, &DataT, (PBYTE)buffer, buffersize, &buffersize ) ) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) { LocalFree(buffer); } buffer = reinterpret_cast<LPTSTR>((char*)LocalAlloc(LPTR,buffersize)); } else { break; } } if ( ( buffer != NULL ) ) { if (buffer) { LocalFree(buffer); } break; } if (buffer) { LocalFree(buffer); } } SP_PROPCHANGE_PARAMS propChange = { sizeof ( SP_CLASSINSTALL_HEADER ) }; propChange.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; propChange.Scope = DICS_FLAG_GLOBAL; propChange.StateChange = DICS_DISABLE; // 启用DICS_ENABLE ,DICS_DISABLE GUID uid = m_DeviceInfoData.ClassGuid; if (m_DeviceInfoData.DevInst != NULL) { rlt = SetupDiSetClassInstallParams ( m_hDevInfo, &m_DeviceInfoData, ( SP_CLASSINSTALL_HEADER * ) &propChange, sizeof ( propChange ) ); } cout << "0000000" << rlt; if ( rlt ) { rlt = SetupDiCallClassInstaller ( DIF_PROPERTYCHANGE, m_hDevInfo, &m_DeviceInfoData ); } cout << "11111" << rlt; cout << GetLastError(); } */ 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; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1137