C++实现获取设备管理器中的设备信息,基本调用了windows API函数,除此之外,还引用了setupapi.lib库,代码如下所示:
// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <locale.h>
#include <Windows.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
void PrintDevicesInfo1()
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs Err:%d", GetLastError());
return;
};
SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
SetupDiGetClassImageList(&_spImageData);
SP_DEVINFO_DATA spDevInfoData = { 0 };
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
{
char szBuf[MAX_PATH] = { 0 };
int wImageIdx = 0;
short wItem = 0;
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0))
{
continue;
};
if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
{
char szName[MAX_PATH] = { 0 };
DWORD dwRequireSize;
if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, (PWSTR)szBuf, MAX_PATH, &dwRequireSize))
{
continue;
};
wprintf(L"Class:%s\r\n", szBuf);
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
wprintf(L"FriendlyName:%s\r\n\r\n", szName);
}
else if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
wprintf(L"Device:%s\r\n\r\n", szName);
}
}
}
SetupDiDestroyClassImageList(&_spImageData);
}
int PrintDevicesInfo2()
{
// 得到所有设备 HDEVINFO
HDEVINFO hDevInfo;
hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
wprintf(L"SetupDiGetClassDevs Err:%d", GetLastError());
return -1;
};
// 循环列举
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
WCHAR szClassBuf[MAX_PATH] = { 0 };
WCHAR szDescBuf[MAX_PATH] = { 0 };
WCHAR locinfo[MAX_PATH] = { 0 };
WCHAR friendName[MAX_PATH] = { 0 };
// 获取类名
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
{
}
//获取设备描述信息
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
{
}
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION, NULL, (PBYTE)locinfo, sizeof(locinfo), NULL))
{
}
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendName, sizeof(friendName), NULL))
{
}
wprintf(L"Class:%s\r\nDesc:%s\nLocoInfo:%s\nFriendName:%s\r\n\r\n", szClassBuf, szDescBuf, locinfo,friendName);
}
// 释放
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
int main(int argc, char* argv[])
{
printf("Locale is: %s\n", setlocale(LC_ALL, "chs"));
PrintDevicesInfo1();
//PrintDevicesInfo2();
getchar();
return 0;
}
// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <locale.h>
#include <Windows.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
void PrintDevicesInfo1()
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs Err:%d", GetLastError());
return;
};
SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
SetupDiGetClassImageList(&_spImageData);
SP_DEVINFO_DATA spDevInfoData = { 0 };
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
{
char szBuf[MAX_PATH] = { 0 };
int wImageIdx = 0;
short wItem = 0;
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (PBYTE)szBuf, MAX_PATH, 0))
{
continue;
};
if (SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
{
char szName[MAX_PATH] = { 0 };
DWORD dwRequireSize;
if (!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, (PWSTR)szBuf, MAX_PATH, &dwRequireSize))
{
continue;
};
wprintf(L"Class:%s\r\n", szBuf);
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
wprintf(L"FriendlyName:%s\r\n\r\n", szName);
}
else if (SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
wprintf(L"Device:%s\r\n\r\n", szName);
}
}
}
SetupDiDestroyClassImageList(&_spImageData);
}
int PrintDevicesInfo2()
{
// 得到所有设备 HDEVINFO
HDEVINFO hDevInfo;
hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
wprintf(L"SetupDiGetClassDevs Err:%d", GetLastError());
return -1;
};
// 循环列举
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
WCHAR szClassBuf[MAX_PATH] = { 0 };
WCHAR szDescBuf[MAX_PATH] = { 0 };
WCHAR locinfo[MAX_PATH] = { 0 };
WCHAR friendName[MAX_PATH] = { 0 };
// 获取类名
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
{
}
//获取设备描述信息
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
{
}
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_LOCATION_INFORMATION, NULL, (PBYTE)locinfo, sizeof(locinfo), NULL))
{
}
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)friendName, sizeof(friendName), NULL))
{
}
wprintf(L"Class:%s\r\nDesc:%s\nLocoInfo:%s\nFriendName:%s\r\n\r\n", szClassBuf, szDescBuf, locinfo,friendName);
}
// 释放
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
int main(int argc, char* argv[])
{
printf("Locale is: %s\n", setlocale(LC_ALL, "chs"));
PrintDevicesInfo1();
//PrintDevicesInfo2();
getchar();
return 0;
}
运行结果如下所示:
标签:spDevInfoData,管理器,MAX,C++,hDevInfo,SPDRP,PATH,NULL,设备 From: https://blog.csdn.net/qq_57661075/article/details/139245002