在Windows中对USB设备需要进行禁止或者放开禁止。据我所知一般采用三种方式,一种是通过驱动从操作系统的总线层进行过滤,其二是在设备管理器中禁用,其三是调用系统API。
注:第二种本质也是调用系统API。
从设备管理器中禁用/启用
主要是对Windows程序devconx64.exe或者devconx86.exe带参数调用。调用方式参照链接:https://www.jianshu.com/p/5e107c061ce4中的示例。
devcon是微软的开源程序,源码是:https://github.com/kiibohd/kiidrv/blob/master/devcon/cmds.cpp
微软说明的路径:https://docs.microsoft.com/zh-tw/windows-hardware/drivers/devtest/devcon
里面有详细的使用方式、下载方式和示例。调用系统API
#include <iostream>
#include <WINDOWS.H>
#include <atlstr.h>
#include <TCHAR.H>
#include <SETUPAPI.H>
#include <rpcdce.h>
#pragma comment(lib, "Rpcrt4.lib")
#pragma comment(lib, "setupapi.lib")
using namespace std;
HDEVINFO hDevInfo = NULL;
void FormatMSG(DWORD dwError, LPTSTR * o)
{
LPTSTR oo = (LPTSTR)o;
BOOL bOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError,
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), oo, 0, NULL);
if (!bOk)
{
HMODULE hDll = LoadLibraryEx(_T("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
if (NULL != hDll)
{
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
FORMAT_MESSAGE_FROM_SYSTEM, hDll,
dwError,
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),
(LPTSTR)o, 0, NULL);
FreeLibrary(hDll);
}
}
}
BOOL ChangeStatus(DWORD NewStatus, DWORD SelectedItem, HDEVINFO hDevInfo)
{
LPTSTR lpszMsg = NULL;
try
{
SP_PROPCHANGE_PARAMS PropChangeParams = { sizeof(SP_CLASSINSTALL_HEADER) };
SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
// Get a handle to the Selected Item.
if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, &DeviceInfoData))
{
FormatMSG(GetLastError(), &lpszMsg);
throw lpszMsg;
}
// Set the PropChangeParams structure.
PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewStatus;
if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
{
FormatMSG(GetLastError(), &lpszMsg);
throw lpszMsg;
}
// Call the ClassInstaller and perform the change.
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))
{
FormatMSG(GetLastError(), &lpszMsg);
throw lpszMsg;
}
return TRUE;
}
catch(TCHAR * pszError)
{
CString str = CString(pszError);
wcout << "ChangeStatus mention : " << (LPCTSTR)str << endl;
if (NULL != lpszMsg)
{
LocalFree((HLOCAL)lpszMsg);
}
return FALSE;
}
}
BOOL ControlDisk(int nStatus, int nIndex)
{
if(-1 == nStatus)
{
return FALSE;
}
LPTSTR lpszMsg = NULL;
try
{
TCHAR * GUIDString = NULL;
GUID guid;
ZeroMemory(&guid, sizeof(GUID));
switch (nIndex)
{
case 0 : // 0 代表软驱
GUIDString = _T("4D36E980-E325-11CE-BFC1-08002BE10318");
UuidFromStringW((RPC_WSTR)GUIDString, &guid);
break;
case 1: // 1 代表光驱
GUIDString = _T("4D36E965-E325-11CE-BFC1-08002BE10318");
UuidFromStringW((RPC_WSTR)GUIDString, &guid);
break;
case 2 : // 2 代表USB
GUIDString = _T("36FC9E60-C465-11CF-8056-444553540000");
UuidFromStringW((RPC_WSTR)GUIDString, &guid);
break;
}
hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT);
if(INVALID_HANDLE_VALUE == hDevInfo)
{
lpszMsg = L"hahazx";
FormatMSG(GetLastError(), &lpszMsg);
throw lpszMsg;
}
DWORD i;
SP_DEVINFO_DATA DeviceInfoData;
ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0 ; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); ++i)
{
if(1 == nStatus)
{
//::MessageBox(NULL, L"启用", L"correct", MB_OK);
cout << "======start======" << endl;
ChangeStatus(DICS_ENABLE, i, hDevInfo);
}
else if(0 == nStatus)
{
//::MessageBox(NULL, L"禁用", L"error", MB_OK);
cout << "======forbid======" << endl;
ChangeStatus(DICS_DISABLE, i, hDevInfo);
}
}
// 释放 device information set
return SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch(TCHAR * pszError)
{
CString str = CString(pszError);
//cout << "ControlDisk mention : " << (LPCTSTR)str << endl;
OutputDebugString(str);
if(NULL != lpszMsg)
{
LocalFree((HLOCAL)lpszMsg);
}
return FALSE;
}
return FALSE;
}
int main(void)
{
ControlDisk(0, 2);
//
//Sleep(2000);
//
//ControlDisk(1, 2);
system("pause");
return 0;
}