1. 建个基于Dialog的VC6程序;
2. 添加以下代码到***Dlg.cpp中;
---------------------------------------------------------------------------------------------------
#include <setupapi.h>
#include <shlwapi.h>
#include <Rpcdce.h>
#pragma comment( lib, "Rpcrt4.lib" )
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "shlwapi.lib")
#include <shlwapi.h>
#include <Rpcdce.h>
#pragma comment( lib, "Rpcrt4.lib" )
#pragma comment(lib, "setupapi.lib")
#pragma comment(lib, "shlwapi.lib")
3. 加上两个Button —— Button1 和 Button2; 及函数 OnButton1(), OnButton2();
4. 添加以下代码到***Dlg.cpp中;
----------------------------------------------------------------------------------------------------
/
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);
}
}
}
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;
}
}
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;
}
hDevInfo = SetupDiGetClassDevs(&guid,
NULL,
NULL,
DIGCF_PRESENT);
// 参数说明,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;
}
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)
{
ChangeStatus(DICS_ENABLE, i, hDevInfo);
}
else if (0 == nStatus)
{
ChangeStatus(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;
}
{
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)
{
ChangeStatus(DICS_ENABLE, i, hDevInfo);
}
else if (0 == nStatus)
{
ChangeStatus(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;
}
///
void CForbidCDDlg::OnButton1()
{
{
// 禁用光驱
ControlDisk(0, 1);
}
ControlDisk(0, 1);
}
void CForbidCDDlg::OnButton2()
{
{
// 启用光驱
ControlDisk(1, 1);
}
ControlDisk(1, 1);
}
/
ps: 在编译前,先确认是否安装了DDK且其 inc 和 lib 目录已经被包含到VC设置中。