有些时候需要先获取设备管理器里面的硬件信息,才能执行对应的操作,简单的鼓捣了一下,贴出来共享。
具体有两个方法,大致来说差不多,流程一样 ,懒得整理了,直接贴出来原始体。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// PrintDeviceInfo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#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);
short
wIndex = 0;
SP_DEVINFO_DATA spDevInfoData = {0};
spDevInfoData.cbSize =
sizeof
(SP_DEVINFO_DATA);
while
(1)
{
if
(SetupDiEnumDeviceInfo(hDevInfo, wIndex, &spDevInfoData))
{
char
szBuf[MAX_PATH] = {0};
int
wImageIdx = 0;
short
wItem = 0;
if
(!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_CLASS, NULL, (
PBYTE
)szBuf, MAX_PATH, 0))
{
wIndex++;
continue
;
};
if
(SetupDiGetClassImageIndex(&_spImageData, &spDevInfoData.ClassGuid, &wImageIdx))
{
char
szName[MAX_PATH] = {0};
DWORD
dwRequireSize;
//
if
(!SetupDiGetClassDescription(&spDevInfoData.ClassGuid, szBuf, MAX_PATH, &dwRequireSize))
{
wIndex++;
continue
;
};
printf
(
"Class:%s\r\n"
, szBuf);
if
(SetupDiGetDeviceRegistryProperty(hDevInfo,&spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (
PBYTE
)szName, MAX_PATH - 1, 0))
{
printf
(
"Device:%s\r\n\r\n"
, szName);
}
else
if
(SetupDiGetDeviceRegistryProperty(hDevInfo,&spDevInfoData, SPDRP_DEVICEDESC, NULL, (
PBYTE
)szName, MAX_PATH - 1, 0))
{
printf
(
"Device:%s\r\n\r\n"
, szName);
};
};
}
else
break
;
wIndex++;
};
SetupDiDestroyClassImageList(&_spImageData);
}
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 };
// 获取类名
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
;
printf
(
"Class:%s\r\nDesc:%s\r\n\r\n"
, szClassBuf, szDescBuf);
}
// 释放
SetupDiDestroyDeviceInfoList(hDevInfo);
getchar
();
return
0;
}
|