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.
15extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
16
22
32template<class _Traits, class _Ax>
33static 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
63namespace winstd
64{
67
71 template <class _Ty> struct WlanFreeMemory_delete
72 {
74
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 {
100
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 {
130
131 public:
137 virtual ~wlan_handle()
138 {
139 if (m_h != invalid)
141 }
142
152 __declspec(deprecated("Use WlanOpenHandle - mind it returns error number rather than SetLastError"))
153 bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) noexcept
154 {
155 handle_type h;
156 const DWORD dwResult = WlanOpenHandle(dwClientVersion, 0, pdwNegotiatedVersion, &h);
157 if (dwResult == ERROR_SUCCESS) {
158 attach(h);
159 return true;
160 } else {
161 SetLastError(dwResult);
162 return false;
163 }
164 }
165
166 protected:
172 void free_internal() noexcept override
173 {
174 WlanCloseHandle(m_h, NULL);
175 }
176 };
177
179}
180
183
189#pragma warning(suppress: 4505) // Don't warn on unused code
190static DWORD WlanOpenHandle(
191 _In_ DWORD dwClientVersion,
192 _Reserved_ PVOID pReserved,
193 _Out_ PDWORD pdwNegotiatedVersion,
194 _Inout_ winstd::wlan_handle &handle)
195{
196 HANDLE h;
197 DWORD dwResult = WlanOpenHandle(dwClientVersion, pReserved, pdwNegotiatedVersion, &h);
198 if (dwResult == ERROR_SUCCESS)
199 handle.attach(h);
200 return dwResult;
201}
202
Base abstract template class to support generic object handle keeping.
Definition: Common.h:603
HANDLE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:608
handle_type m_h
Object handle.
Definition: Common.h:854
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:817
WLAN handle wrapper.
Definition: WLAN.h:128
virtual ~wlan_handle()
Closes a connection to the server.
Definition: WLAN.h:137
void free_internal() noexcept override
Closes a connection to the server.
Definition: WLAN.h:172
__declspec(deprecated("Use WlanOpenHandle - mind it returns error number rather than SetLastError")) bool open(DWORD dwClientVersion
Opens a connection to the server.
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:161
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:613
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:104
void operator()(_Other *) const
Delete a pointer of another type.
Definition: WLAN.h:118
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:99
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:109
Deleter for unique_ptr using WlanFreeMemory.
Definition: WLAN.h:72
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:78
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:88
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:73
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition: WLAN.h:83