WinStd
Windows Win32 API using Standard C++
All Classes Functions Variables Typedefs Enumerations Enumerator Modules Pages
WLAN.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 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 SIZE_T sSize = 0;
37
38 if (!::pfnWlanReasonCodeToString)
39 return ERROR_CALL_NOT_IMPLEMENTED;
40
41 for (;;) {
42 // Increment size and allocate buffer.
43 sSize = SIZETAdd(sSize, 1024);
44 if (sSize > DWORD_MAX)
45 throw std::runtime_error("Data too big");
46 sValue.resize(sSize - 1);
47
48 // Try!
49 DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, static_cast<DWORD>(sSize), &sValue[0], pReserved);
50 if (dwResult == ERROR_SUCCESS) {
51 SIZE_T sLength = wcsnlen(&sValue[0], sSize);
52 if (sLength < sSize - 1) {
53 // Buffer was long enough.
54 sValue.resize(sLength);
55 return ERROR_SUCCESS;
56 }
57 } else {
58 // Return error code.
59 return dwResult;
60 }
61 }
62}
63
65
66namespace winstd
67{
70
74 template <class _Ty> struct WlanFreeMemory_delete
75 {
77
82
86 template <class _Ty2> WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {}
87
91 void operator()(_Ty *_Ptr) const
92 {
93 WlanFreeMemory(_Ptr);
94 }
95 };
96
100 template <class _Ty> struct WlanFreeMemory_delete<_Ty[]>
101 {
103
108
112 void operator()(_Ty *_Ptr) const
113 {
114 WlanFreeMemory(_Ptr);
115 }
116
120 template<class _Other>
121 void operator()(_Other *) const
122 {
123 WlanFreeMemory(_Ptr);
124 }
125 };
126
132 class wlan_handle : public handle<HANDLE, NULL>
133 {
134 WINSTD_HANDLE_IMPL(wlan_handle, HANDLE, NULL)
135
136 public:
142 virtual ~wlan_handle()
143 {
144 if (m_h != invalid)
146 }
147
148 protected:
154 void free_internal() noexcept override
155 {
156 WlanCloseHandle(m_h, NULL);
157 }
158 };
159
161}
162
165
171#pragma warning(suppress: 4505) // Don't warn on unused code
172static DWORD WlanOpenHandle(
173 _In_ DWORD dwClientVersion,
174 _Reserved_ PVOID pReserved,
175 _Out_ PDWORD pdwNegotiatedVersion,
176 _Inout_ winstd::wlan_handle &handle)
177{
178 HANDLE h;
179 DWORD dwResult = WlanOpenHandle(dwClientVersion, pReserved, pdwNegotiatedVersion, &h);
180 if (dwResult == ERROR_SUCCESS)
181 handle.attach(h);
182 return dwResult;
183}
184
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
handle_type m_h
Definition Common.h:1126
WLAN handle wrapper.
Definition WLAN.h:133
virtual ~wlan_handle()
Closes a connection to the server.
Definition WLAN.h:142
void free_internal() noexcept override
Closes a connection to the server.
Definition WLAN.h:154
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
static const HANDLE invalid
Definition Common.h:884
static DWORD WlanOpenHandle(DWORD dwClientVersion, PVOID pReserved, PDWORD pdwNegotiatedVersion, winstd::wlan_handle &handle)
Opens a connection to the server.
Definition WLAN.h:172
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< _Ty > _Myt
This type.
Definition WLAN.h:102
WlanFreeMemory_delete()
Default construct.
Definition WLAN.h:107
void operator()(_Other *) const
Delete a pointer of another type.
Definition WLAN.h:121
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition WLAN.h:112
Deleter for unique_ptr using WlanFreeMemory.
Definition WLAN.h:75
WlanFreeMemory_delete()
Default construct.
Definition WLAN.h:81
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition WLAN.h:91
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition WLAN.h:86
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition WLAN.h:76