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 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}
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
HANDLE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
void attach(1 handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
handle_type m_h
Object handle.
Definition: Common.h:855
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:171
bool open(1 DWORD dwClientVersion, 1 PDWORD pdwNegotiatedVersion) noexcept
Opens a connection to the server.
Definition: WLAN.h:152
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:162
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:614
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