blob: 229d7ee493153d0001772709838d63f276b79b75 (
plain)
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
|
#include "stdafx.h"
#include "devenum.h"
#include "error.h"
DeviceEnumerator::DeviceEnumerator(const GUID &deviceClass)
{
m_deviceInfoSet = SetupDiGetClassDevsW
(
&deviceClass,
nullptr,
nullptr,
DIGCF_PRESENT
);
if (INVALID_HANDLE_VALUE == m_deviceInfoSet)
{
THROW_SETUPAPI_ERROR(GetLastError(), "SetupDiGetClassDevsW");
}
m_nextDeviceIndex = 0;
m_exhausted = false;
}
//static
std::unique_ptr<DeviceEnumerator> DeviceEnumerator::Create(const GUID& deviceClass, Filter filter)
{
auto enumerator = std::make_unique<DeviceEnumerator>(deviceClass);
enumerator->setFilter(filter);
return enumerator;
}
DeviceEnumerator::~DeviceEnumerator()
{
SetupDiDestroyDeviceInfoList(m_deviceInfoSet);
}
bool DeviceEnumerator::next(EnumeratedDevice &device)
{
if (m_exhausted)
{
return false;
}
SP_DEVINFO_DATA deviceInfo { 0 };
deviceInfo.cbSize = sizeof(deviceInfo);
for (;;)
{
if (FALSE == SetupDiEnumDeviceInfo(m_deviceInfoSet, m_nextDeviceIndex, &deviceInfo))
{
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
THROW_SETUPAPI_ERROR(GetLastError(), "SetupDiEnumDeviceInfo");
}
m_exhausted = true;
return false;
}
++m_nextDeviceIndex;
if (!m_filter || m_filter(m_deviceInfoSet, deviceInfo))
{
break;
}
}
device.deviceInfoSet = m_deviceInfoSet;
device.deviceInfo = deviceInfo;
return true;
}
|