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

WLAN.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 <wlanapi.h>
11#include <string>
12
13// Must not statically link to Wlanapi.dll as it is not available on Windows
14// without a WLAN interface.
15 extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
16
22
32 template<class _Traits, class _Ax>
33 static DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sValue, __reserved PVOID pReserved)
34{
35 DWORD dwSize = 0;
36
37 if (!::pfnWlanReasonCodeToString)
38 return ERROR_CALL_NOT_IMPLEMENTED;
39
40 for (;;) {
41 // Increment size and allocate buffer.
42 dwSize += 1024;
43 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
44
45 // Try!
46 DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, dwSize, szBuffer.get(), pReserved);
47 if (dwResult == ERROR_SUCCESS) {
48 DWORD dwLength = (DWORD)wcsnlen(szBuffer.get(), dwSize);
49 if (dwLength < dwSize - 1) {
50 // Buffer was long enough.
51 sValue.assign(szBuffer.get(), dwLength);
52 return ERROR_SUCCESS;
53 }
54 } else {
55 // Return error code.
56 return dwResult;
57 }
58 }
59}
60
62
63 namespace winstd
64{
67
71 template <class _Ty> struct WlanFreeMemory_delete
72 {
73 typedef WlanFreeMemory_delete<_Ty> _Myt;
74
78 WlanFreeMemory_delete() {}
79
83 template <class _Ty2> WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {}
84
88 void operator()(_Ty *_Ptr) const
89 {
90 WlanFreeMemory(_Ptr);
91 }
92 };
93
97 template <class _Ty> struct WlanFreeMemory_delete<_Ty[]>
98 {
99 typedef WlanFreeMemory_delete<_Ty> _Myt;
100
104 WlanFreeMemory_delete() {}
105
109 void operator()(_Ty *_Ptr) const
110 {
111 WlanFreeMemory(_Ptr);
112 }
113
117 template<class _Other>
118 void operator()(_Other *) const
119 {
120 WlanFreeMemory(_Ptr);
121 }
122 };
123
127 class wlan_handle : public handle<HANDLE, NULL>
128 {
129 WINSTD_HANDLE_IMPL(wlan_handle, NULL)
130
131 public:
137 virtual ~wlan_handle()
138 {
139 if (m_h != invalid)
140 free_internal();
141 }
142
152 bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) noexcept
153 {
154 handle_type h;
155 const DWORD dwResult = WlanOpenHandle(dwClientVersion, 0, pdwNegotiatedVersion, &h);
156 if (dwResult == ERROR_SUCCESS) {
157 attach(h);
158 return true;
159 } else {
160 SetLastError(dwResult);
161 return false;
162 }
163 }
164
165 protected:
171 void free_internal() noexcept override
172 {
173 WlanCloseHandle(m_h, NULL);
174 }
175 };
176
178}
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
winstd::handle< HANDLE, NULL >::handle_type
HANDLE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
winstd::handle< HANDLE, NULL >::attach
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
winstd::handle< HANDLE, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:833
winstd::wlan_handle
WLAN handle wrapper.
Definition: WLAN.h:128
winstd::wlan_handle::open
bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) noexcept
Opens a connection to the server.
Definition: WLAN.h:152
winstd::wlan_handle::~wlan_handle
virtual ~wlan_handle()
Closes a connection to the server.
Definition: WLAN.h:137
winstd::wlan_handle::free_internal
void free_internal() noexcept override
Closes a connection to the server.
Definition: WLAN.h:171
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< HANDLE, NULL >::invalid
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:590
winstd::WlanFreeMemory_delete< _Ty[]>::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:104
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition: WLAN.h:118
winstd::WlanFreeMemory_delete< _Ty[]>::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:99
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:109
winstd::WlanFreeMemory_delete
Deleter for unique_ptr using WlanFreeMemory.
Definition: WLAN.h:72
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:78
winstd::WlanFreeMemory_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:88
winstd::WlanFreeMemory_delete::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:73
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition: WLAN.h:83