WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
WLAN.h
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
12
13#pragma once
14
15#include "Common.h"
16#include <wlanapi.h>
17#include <string>
18
21
23// Must not statically link to Wlanapi.dll as it is not available on Windows
24// without a WLAN interface.
25extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
27
37template<class _Traits, class _Ax>
38static DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sValue, __reserved PVOID pReserved)
39{
40 DWORD dwSize = 0;
41
42 if (!::pfnWlanReasonCodeToString)
43 return ERROR_CALL_NOT_IMPLEMENTED;
44
45 for (;;) {
46 // Increment size and allocate buffer.
47 dwSize += 1024;
48 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
49
50 // Try!
51 DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, dwSize, szBuffer.get(), pReserved);
52 if (dwResult == ERROR_SUCCESS) {
53 DWORD dwLength = (DWORD)wcsnlen(szBuffer.get(), dwSize);
54 if (dwLength < dwSize - 1) {
55 // Buffer was long enough.
56 sValue.assign(szBuffer.get(), dwLength);
57 return ERROR_SUCCESS;
58 }
59 } else {
60 // Return error code.
61 return dwResult;
62 }
63 }
64}
65
67
68namespace winstd
69{
72
76 template <class _Ty> struct WlanFreeMemory_delete
77 {
79
84
88 template <class _Ty2> WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {}
89
93 void operator()(_Ty *_Ptr) const
94 {
95 WlanFreeMemory(_Ptr);
96 }
97 };
98
102 template <class _Ty> struct WlanFreeMemory_delete<_Ty[]>
103 {
105
110
114 void operator()(_Ty *_Ptr) const
115 {
116 WlanFreeMemory(_Ptr);
117 }
118
122 template<class _Other>
123 void operator()(_Other *) const
124 {
125 WlanFreeMemory(_Ptr);
126 }
127 };
128
134 class wlan_handle : public handle<HANDLE, NULL>
135 {
137
138 public:
144 virtual ~wlan_handle()
145 {
146 if (m_h != invalid)
148 }
149
150 protected:
156 void free_internal() noexcept override
157 {
158 WlanCloseHandle(m_h, NULL);
159 }
160 };
161
163}
164
167
173#pragma warning(suppress: 4505) // Don't warn on unused code
174static DWORD WlanOpenHandle(
175 _In_ DWORD dwClientVersion,
176 _Reserved_ PVOID pReserved,
177 _Out_ PDWORD pdwNegotiatedVersion,
178 _Inout_ winstd::wlan_handle &handle)
179{
180 HANDLE h;
181 DWORD dwResult = WlanOpenHandle(dwClientVersion, pReserved, pdwNegotiatedVersion, &h);
182 if (dwResult == ERROR_SUCCESS)
183 handle.attach(h);
184 return dwResult;
185}
186
General API.
Base abstract template class to support generic object handle keeping.
Definition: Common.h:615
handle_type m_h
Object handle.
Definition: Common.h:866
WLAN handle wrapper.
Definition: WLAN.h:135
virtual ~wlan_handle()
Closes a connection to the server.
Definition: WLAN.h:144
void free_internal() noexcept override
Closes a connection to the server.
Definition: WLAN.h:156
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:171
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:625
static DWORD WlanOpenHandle(DWORD dwClientVersion, PVOID pReserved, PDWORD pdwNegotiatedVersion, winstd::wlan_handle &handle)
Opens a connection to the server.
Definition: WLAN.h:174
static DWORD WlanReasonCodeToString(DWORD dwReasonCode, std::basic_string< wchar_t, _Traits, _Ax > &sValue, __reserved PVOID pReserved)
Retrieves a string that describes a specified reason code and stores it in a std::wstring string.
Definition: WLAN.h:38
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:109
void operator()(_Other *) const
Delete a pointer of another type.
Definition: WLAN.h:123
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:104
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:114
Deleter for unique_ptr using WlanFreeMemory.
Definition: WLAN.h:77
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:83
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:93
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:78
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition: WLAN.h:88