1.枚举系统设备
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;return TRUE;
//根据设备ID判断指定设备是否可禁用
BOOL IsDisableable(DWORD dwDevID, HDEVINFO hDevInfo)
{
SP_DEVINFO_DATA DevInfoData = { sizeof(SP_DEVINFO_DATA) };
DWORD dwDevStatus, dwProblem;
if (!SetupDiEnumDeviceInfo(hDevInfo, dwDevID, &DevInfoData))
{
OutputDebugString(_T("SetupDiEnumDeviceInfo FAILED"));
return FALSE;
}
//查询设备状态
if (CM_Get_DevNode_Status(&dwDevStatus, &dwProblem, DevInfoData.DevInst, 0) != CR_SUCCESS)
{
OutputDebugString(_T("CM_GET_DevNode_Status FAILED"));
return FALSE;
}
return ((dwDevStatus & DN_DISABLEABLE) && (dwProblem != CM_PROB_HARDWARE_DISABLED));
}
//根据设备ID判断指定设备是否已禁用
BOOL IsDisabled(DWORD dwDevID, HDEVINFO hDevInfo)
{
SP_DEVINFO_DATA DevInfoData = { sizeof(SP_DEVINFO_DATA) };
DWORD dwDevStatus, dwProblem;
if (!SetupDiEnumDeviceInfo(hDevInfo, dwDevID, &DevInfoData))
{
OutputDebugString(_T("SetupDiEnumDeviceInfo FAILED"));
return FALSE;
}
//查询设备状态
if (CM_Get_DevNode_Status(&dwDevStatus, &dwProblem, DevInfoData.DevInst, 0) != CR_SUCCESS)
{
OutputDebugString(_T("CM_GET_DevNode_Status FAILED"));
return FALSE;
}
return ((dwDevStatus & DN_HAS_PROBLEM) && (dwProblem == CM_PROB_DISABLED));
}
//根据设备ID以及dwNewState:DICS_ENABLE,DICS_DISABLE两个参数来启用,禁用设备
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))
{
OutputDebugString(_T("SetupDiEnumDeviceInfo FAILED"));
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)))
{
OutputDebugString(_T("SetupDiSetClassInstallParams FAILED"));
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))
{
OutputDebugString(_T("SetupDiSetClassInstallParams or SetupDiCallClassInstaller FAILED"));
return TRUE;
}
else
{
//判断是否需要重新启动
devParams.cbSize = sizeof(devParams);
if (!SetupDiGetDeviceInstallParams(hDevInfo, &DevInfoData, &devParams))
{
OutputDebugString(_T("SetupDiGetDeviceInstallParams FAILED"));
return FALSE;
}
if (devParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
{
OutputDebugString(_T("Need Restart Computer"));
return TRUE;
}
return TRUE;
}
}