WinStd
Windows Win32 API using Standard C++
WLAN.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <wlanapi.h>
13#include <string>
14
17
19// Must not statically link to Wlanapi.dll as it is not available on Windows
20// without a WLAN interface.
21extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
23
33template<class _Traits, class _Ax>
34static DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sValue, __reserved PVOID pReserved)
35{
36 DWORD dwSize = 0;
37
38 if (!::pfnWlanReasonCodeToString)
39 return ERROR_CALL_NOT_IMPLEMENTED;
40
41 for (;;) {
42 // Increment size and allocate buffer.
43 dwSize += 1024;
44 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
45
46 // Try!
47 DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, dwSize, szBuffer.get(), pReserved);
48 if (dwResult == ERROR_SUCCESS) {
49 DWORD dwLength = (DWORD)wcsnlen(szBuffer.get(), dwSize);
50 if (dwLength < dwSize - 1) {
51 // Buffer was long enough.
52 sValue.assign(szBuffer.get(), dwLength);
53 return ERROR_SUCCESS;
54 }
55 } else {
56 // Return error code.
57 return dwResult;
58 }
59 }
60}
61
63
64namespace winstd
65{
68
72 template <class _Ty> struct WlanFreeMemory_delete
73 {
75
80
84 template <class _Ty2> WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {}
85
89 void operator()(_Ty *_Ptr) const
90 {
91 WlanFreeMemory(_Ptr);
92 }
93 };
94
98 template <class _Ty> struct WlanFreeMemory_delete<_Ty[]>
99 {
101
106
110 void operator()(_Ty *_Ptr) const
111 {
112 WlanFreeMemory(_Ptr);
113 }
114
118 template<class _Other>
119 void operator()(_Other *) const
120 {
121 WlanFreeMemory(_Ptr);
122 }
123 };
124
130 class wlan_handle : public handle<HANDLE, NULL>
131 {
133
134 public:
140 virtual ~wlan_handle()
141 {
142 if (m_h != invalid)
144 }
145
146 protected:
152 void free_internal() noexcept override
153 {
154 WlanCloseHandle(m_h, NULL);
155 }
156 };
157
159}
160
163
169#pragma warning(suppress: 4505) // Don't warn on unused code
170static DWORD WlanOpenHandle(
171 _In_ DWORD dwClientVersion,
172 _Reserved_ PVOID pReserved,
173 _Out_ PDWORD pdwNegotiatedVersion,
174 _Inout_ winstd::wlan_handle &handle)
175{
176 HANDLE h;
177 DWORD dwResult = WlanOpenHandle(dwClientVersion, pReserved, pdwNegotiatedVersion, &h);
178 if (dwResult == ERROR_SUCCESS)
179 handle.attach(h);
180 return dwResult;
181}
182
Base abstract template class to support generic object handle keeping.
Definition: Common.h:415
handle_type m_h
Object handle.
Definition: Common.h:666
WLAN handle wrapper.
Definition: WLAN.h:131
virtual ~wlan_handle()
Closes a connection to the server.
Definition: WLAN.h:140
void free_internal() noexcept override
Closes 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:163
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:425
static DWORD WlanOpenHandle(DWORD dwClientVersion, PVOID pReserved, PDWORD pdwNegotiatedVersion, winstd::wlan_handle &handle)
Opens a connection to the server.
Definition: WLAN.h:170
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:34
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:105
void operator()(_Other *) const
Delete a pointer of another type.
Definition: WLAN.h:119
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:100
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:110
Deleter for unique_ptr using WlanFreeMemory.
Definition: WLAN.h:73
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:79
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:89
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:74
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition: WLAN.h:84