WinStd Additional templates and function helpers for Microsoft Windows using Standard C++ classes

SetupAPI.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "Common.h"
10#include <SetupAPI.h>
11
12 namespace winstd
13{
19
23 class setup_device_info_list : public handle<HDEVINFO, INVALID_HANDLE_VALUE>
24 {
25 WINSTD_HANDLE_IMPL(setup_device_info_list, INVALID_HANDLE_VALUE)
26
27 public:
33 virtual ~setup_device_info_list()
34 {
35 if (m_h != invalid)
36 free_internal();
37 }
38
48 bool create(
49 _In_opt_ const GUID * ClassGuid,
50 _In_opt_ HWND hwndParent) noexcept
51 {
52 handle_type h = SetupDiCreateDeviceInfoList(ClassGuid, hwndParent);
53 if (h != invalid) {
54 attach(h);
55 return true;
56 }
57 else
58 return false;
59 }
60
70 bool create(
71 _In_opt_ const GUID * ClassGuid,
72 _In_opt_ PCTSTR Enumerator,
73 _In_opt_ HWND hwndParent,
74 _In_ DWORD Flags,
75 _In_opt_ HDEVINFO DeviceInfoSet,
76 _In_opt_ PCTSTR MachineName,
77 _Reserved_ PVOID Reserved) noexcept
78 {
79 handle_type h = SetupDiGetClassDevsEx(ClassGuid, Enumerator, hwndParent, Flags, DeviceInfoSet, MachineName, Reserved);
80 if (h != invalid) {
81 attach(h);
82 return true;
83 }
84 else
85 return false;
86 }
87
88 protected:
94 void free_internal() noexcept override
95 {
96 SetupDiDestroyDeviceInfoList(m_h);
97 }
98 };
99
103 class setup_driver_info_list_builder
104 {
105 WINSTD_NONCOPYABLE(setup_driver_info_list_builder)
106 WINSTD_NONMOVABLE(setup_driver_info_list_builder)
107
108 public:
114 setup_driver_info_list_builder(
115 _In_ HDEVINFO DeviceInfoSet,
116 _Inout_opt_ PSP_DEVINFO_DATA DeviceInfoData,
117 _In_ DWORD DriverType) noexcept :
118 m_DeviceInfoSet (DeviceInfoSet),
119 m_DeviceInfoData(DeviceInfoData),
120 m_DriverType (DriverType)
121 {
122 m_result = SetupDiBuildDriverInfoList(m_DeviceInfoSet, m_DeviceInfoData, m_DriverType);
123 }
124
130 virtual ~setup_driver_info_list_builder()
131 {
132 if (m_result)
133 SetupDiDestroyDriverInfoList(m_DeviceInfoSet, m_DeviceInfoData, m_DriverType);
134 }
135
141 BOOL status() const noexcept
142 {
143 return m_result;
144 }
145
146 protected:
147 HDEVINFO m_DeviceInfoSet;
148 PSP_DEVINFO_DATA m_DeviceInfoData;
149 DWORD m_DriverType;
150 BOOL m_result;
151 };
152
154}
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
winstd::handle< HDEVINFO, INVALID_HANDLE_VALUE >::handle_type
HDEVINFO handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
winstd::handle< HDEVINFO, INVALID_HANDLE_VALUE >::attach
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
winstd::handle< HDEVINFO, INVALID_HANDLE_VALUE >::m_h
handle_type m_h
Object handle.
Definition: Common.h:833
winstd::setup_device_info_list
HDEVINFO wrapper class.
Definition: SetupAPI.h:24
winstd::setup_device_info_list::~setup_device_info_list
virtual ~setup_device_info_list()
Frees the device information set.
Definition: SetupAPI.h:33
winstd::setup_device_info_list::create
bool create(_In_opt_ const GUID *ClassGuid, _In_opt_ PCTSTR Enumerator, _In_opt_ HWND hwndParent, _In_ DWORD Flags, _In_opt_ HDEVINFO DeviceInfoSet, _In_opt_ PCTSTR MachineName, _Reserved_ PVOID Reserved) noexcept
Creates a device information set that contains requested device information elements for a local or a...
Definition: SetupAPI.h:70
winstd::setup_device_info_list::free_internal
void free_internal() noexcept override
Frees the device information set.
Definition: SetupAPI.h:94
winstd::setup_device_info_list::create
bool create(_In_opt_ const GUID *ClassGuid, _In_opt_ HWND hwndParent) noexcept
Creates an empty device information set and optionally associates the set with a device setup class a...
Definition: SetupAPI.h:48
winstd::setup_driver_info_list_builder
Builds a list of drivers in constructor and deletes it in destructor.
Definition: SetupAPI.h:104
winstd::setup_driver_info_list_builder::setup_driver_info_list_builder
setup_driver_info_list_builder(_In_ HDEVINFO DeviceInfoSet, _Inout_opt_ PSP_DEVINFO_DATA DeviceInfoData, _In_ DWORD DriverType) noexcept
Construct the builder and builds a list of drivers that is associated with a specific device or with ...
Definition: SetupAPI.h:114
winstd::setup_driver_info_list_builder::~setup_driver_info_list_builder
virtual ~setup_driver_info_list_builder()
Deletes a driver list and destructs the builder.
Definition: SetupAPI.h:130
winstd::setup_driver_info_list_builder::m_result
BOOL m_result
Did building succeed?
Definition: SetupAPI.h:150
winstd::setup_driver_info_list_builder::status
BOOL status() const noexcept
Return result of SetupDiBuildDriverInfoList() call.
Definition: SetupAPI.h:141
WINSTD_NONCOPYABLE
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:53
WINSTD_NONMOVABLE
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:61
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
winstd::handle< HDEVINFO, INVALID_HANDLE_VALUE >::invalid
static const HDEVINFO invalid
Invalid handle value.
Definition: Common.h:590