WinStd Windows Win32 API using Standard C++

Loading...
Searching...
No Matches
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.
21 extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
23
33 template<class _Traits, class _Ax>
34 static 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
66 namespace winstd
67{
70
74 template <class _Ty> struct WlanFreeMemory_delete
75 {
76 typedef WlanFreeMemory_delete<_Ty> _Myt;
77
81 WlanFreeMemory_delete() {}
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 {
102 typedef WlanFreeMemory_delete<_Ty> _Myt;
103
107 WlanFreeMemory_delete() {}
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)
145 free_internal();
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
172 static 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
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
winstd::handle< HANDLE, NULL >::m_h
handle_type m_h
Object handle.
Definition Common.h:1276
winstd::ref_unique_ptr
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:863
winstd::wlan_handle
WLAN handle wrapper.
Definition WLAN.h:133
winstd::wlan_handle::~wlan_handle
virtual ~wlan_handle()
Closes a connection to the server.
Definition WLAN.h:142
winstd::wlan_handle::free_internal
void free_internal() noexcept override
Closes a connection to the server.
Definition WLAN.h:154
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
winstd::handle< HANDLE, NULL >::invalid
static const HANDLE invalid
Invalid handle value.
Definition Common.h:1034
WlanOpenHandle
static DWORD WlanOpenHandle(DWORD dwClientVersion, PVOID pReserved, PDWORD pdwNegotiatedVersion, winstd::wlan_handle &handle)
Opens a connection to the server.
Definition WLAN.h:172
WlanReasonCodeToString
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
winstd::WlanFreeMemory_delete< _Ty[]>::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition WLAN.h:107
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition WLAN.h:121
winstd::WlanFreeMemory_delete< _Ty[]>::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition WLAN.h:102
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition WLAN.h:112
winstd::WlanFreeMemory_delete
Deleter for unique_ptr using WlanFreeMemory.
Definition WLAN.h:75
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition WLAN.h:81
winstd::WlanFreeMemory_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition WLAN.h:91
winstd::WlanFreeMemory_delete::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition WLAN.h:76
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition WLAN.h:86