參考
Device Management Functions
// 必要的頭文件和要鏈接的LIB文件
#include <setupapi.h>
#include <shlwapi.h>
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "shlwapi.lib") // device information set(我把它譯為設備信息集)
HDEVINFO hDevInfo = NULL;// 出錯信息
void FormatMSG(DWORD dwError, LPTSTR * lpszMsg)
{
BOOL bOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError,
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), (LPTSTR) lpszMsg, 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)lpszMsg,0, NULL);
FreeLibrary(hDll);
}
}
}
<!--[if !supportEmptyParas]--><!--[endif]-->
BOOL ChangeStatus(DWORD NewStatus, DWORD SelectedItem, HDEVINFO hDevInfo)
{
LPTSTR lpszMsg = NULL; HCURSOR hCursor = NULL;
try
{
SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof (SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT)); //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;
}
SetCursor(hCursor);
return TRUE;
}
catch (TCHAR * pszError)
{
SetCursor(hCursor);
::MessageBox(NULL,pszError,_T("提示"),MB_OK);
if (NULL != lpszMsg)
{
LocalFree((HLOCAL)lpszMsg);
}
return FALSE;
}
}
// 這些設備的啟用和禁用主要有此函數實現
// 參數說明,nStatus 可取3個值,-1 :啥也不做// 0 禁用設備,1啟用設備。
// nIndex用於控制是對光驅,軟驅還是對USB啟用或禁用
// 也是可取3個值,0 代表軟驅,1 代表光驅 2 代表USB
// 譬如要對光驅實現禁用,可以這樣調用此函數
// ControlDisk(0, 1);
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");
UuidFromString((unsigned char *)GUIDString, &guid);
break;
case 1: // 1 代表光驅
GUIDString = _T("4D36E965-E325-11CE-BFC1- 08002BE10318");
UuidFromString((unsigned char *)GUIDString, &guid);
break;
case 2: // 2 代表USB
GUIDString = _T("36FC9E60-C465-11CF-8056- 444553540000");
UuidFromString((unsigned char *)GUIDString, &guid);
break;
default:;
}
hDevInfo = SetupDiGetClassDevs (&guid,NULL,NULL,DIGCF_PRESENT);
if (INVALID_HANDLE_VALUE == hDevInfo)
{
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)
{
StateChange(DICS_ENABLE, i, hDevInfo);
}
else if (0 == nStatus)
{
StateChange(DICS_DISABLE, i, hDevInfo);
}
} // 釋放 device information set
return SetupDiDestroyDeviceInfoList(hDevInfo);
}
catch (TCHAR * pszError)
{
::MessageBox(NULL,pszError,_T("提示"),MB_OK);
if (NULL != lpszMsg)
{
LocalFree((HLOCAL)lpszMsg);
}
return FALSE;
}
return FALSE;
}
經測試這樣對光驅,軟驅和USB實現禁用沒有問題,但是當禁用過之後如果要 對USB實現啟用必須兩次調用ControlDisk(1, 2);這個函數才可,其余的兩個啟用 沒什麼問題。還有要說的就是我不是用的枚舉所有的設備,然後再過濾(網上的 那個禁用網卡的就是這種方法)。
據我觀察,在注冊表的此項下下:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\
有很多GUID,每個GUID代表一個設備,譬如這個是USB的
{36FC9E60-C465-11CF-8056-444553540000}
這個是CDROM的
{4D36E965-E325-11CE-BFC1-08002BE10318}