C++ 实现Windows系统设备枚举

5 篇文章 0 订阅
2 篇文章 0 订阅

1.枚举系统设备

HDEVINFO hDevInfo;

SP_DEVINFO_DATA DeviceInfoData;
DWORD dwDevID;

// 得到所有设备 HDEVINFO 
hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES); //根据此处参数不同可分类枚举设备,这里枚举了所有设备
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return FALSE;
}
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (dwDevID = 0; SetupDiEnumDeviceInfo(hDevInfo, dwDevID, &DeviceInfoData); dwDevID++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,//设备名
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// 内存不足 
if (buffer)
HeapFree(GetProcessHeap(), 0, buffer);
buffer = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, buffersize);
}
else
break;
}
TCHAR  strguid[512] = _T("");//设备GUID
_stprintf_s(strguid, _T("%x-%x-%x-%x%x%x%x%x%x%x%x "),
DeviceInfoData.ClassGuid.Data1,
DeviceInfoData.ClassGuid.Data2,
DeviceInfoData.ClassGuid.Data3,
DeviceInfoData.ClassGuid.Data4[0],
DeviceInfoData.ClassGuid.Data4[1],
DeviceInfoData.ClassGuid.Data4[2],
DeviceInfoData.ClassGuid.Data4[3],
DeviceInfoData.ClassGuid.Data4[4],
DeviceInfoData.ClassGuid.Data4[5],
DeviceInfoData.ClassGuid.Data4[6],
DeviceInfoData.ClassGuid.Data4[7]);
deviceinfo.DevID = dwDevID;//设备ID
deviceinfo.bDisabled = IsDisabled(dwDevID, hDevInfo);//是否已禁用
deviceinfo.bDisableable = IsDisableable(dwDevID, hDevInfo);//是否可禁用
wcscpy_s(deviceinfo.DeviceName, buffer);,//设备名
if (buffer)
HeapFree(GetProcessHeap(), 0, buffer);
}
if (GetLastError() != NO_ERROR &&
GetLastError() != ERROR_NO_MORE_ITEMS)
{
return FALSE;
}
SetupDiDestroyDeviceInfoList(hDevInfo);

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;
}




}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值