VS2015 编译通过,显示设备管理器中有错误的设备,代码如下:
// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#pragma comment(lib, "setupapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// 得到所有设备 HDEVINFO
hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
return 0;
// 循环列举
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
char szClassBuf[MAX_PATH] = { 0 };
char szDescBuf[MAX_PATH] = { 0 };
DWORD dwDevStatus, dwProblem;
// 获取类名
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
continue;
//获取设备描述信息
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
continue;
CM_Get_DevNode_Status(&dwDevStatus, &dwProblem, DeviceInfoData.DevInst, 0);
if (dwProblem != 0) {
wprintf(L"Below device has a error:\r\n Class:%s\r\n Desc:%s\r\n\r\n", szClassBuf, szDescBuf);
}
}
// 释放
SetupDiDestroyDeviceInfoList(hDevInfo);
wprintf(L"Press anykey to exit.");
getchar();
return 0;
}
参考:
1.https://blog.csdn.net/flyingleo1981/article/details/53525060 获取设备管理器的信息 – VC
2.https://blog.csdn.net/d2262272d/article/details/105047066 判断usb硬件的驱动是否已安装