WLAN.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2022 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <wlanapi.h>
#include <string>
// Must not statically link to Wlanapi.dll as it is not available on Windows
// without a WLAN interface.
extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in DWORD dwBufferSize, __in_ecount(dwBufferSize) PWCHAR pStringBuffer, __reserved PVOID pReserved);
template<class _Traits, class _Ax>
static DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Inout_ std::basic_string<wchar_t, _Traits, _Ax> &sValue, __reserved PVOID pReserved)
{
DWORD dwSize = 0;
if (!::pfnWlanReasonCodeToString)
return ERROR_CALL_NOT_IMPLEMENTED;
for (;;) {
// Increment size and allocate buffer.
dwSize += 1024;
std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
// Try!
DWORD dwResult = ::pfnWlanReasonCodeToString(dwReasonCode, dwSize, szBuffer.get(), pReserved);
if (dwResult == ERROR_SUCCESS) {
DWORD dwLength = (DWORD)wcsnlen(szBuffer.get(), dwSize);
if (dwLength < dwSize - 1) {
// Buffer was long enough.
sValue.assign(szBuffer.get(), dwLength);
return ERROR_SUCCESS;
}
} else {
// Return error code.
return dwResult;
}
}
}
namespace winstd
{
template <class _Ty> struct WlanFreeMemory_delete
{
typedef WlanFreeMemory_delete<_Ty> _Myt;
WlanFreeMemory_delete() {}
template <class _Ty2> WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {}
void operator()(_Ty *_Ptr) const
{
WlanFreeMemory(_Ptr);
}
};
template <class _Ty> struct WlanFreeMemory_delete<_Ty[]>
{
typedef WlanFreeMemory_delete<_Ty> _Myt;
WlanFreeMemory_delete() {}
void operator()(_Ty *_Ptr) const
{
WlanFreeMemory(_Ptr);
}
template<class _Other>
void operator()(_Other *) const
{
WlanFreeMemory(_Ptr);
}
};
class wlan_handle : public handle<HANDLE, NULL>
{
WINSTD_HANDLE_IMPL(wlan_handle, NULL)
public:
virtual ~wlan_handle()
{
if (m_h != invalid)
free_internal();
}
bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) noexcept
{
handle_type h;
const DWORD dwResult = WlanOpenHandle(dwClientVersion, 0, pdwNegotiatedVersion, &h);
if (dwResult == ERROR_SUCCESS) {
attach(h);
return true;
} else {
SetLastError(dwResult);
return false;
}
}
protected:
void free_internal() noexcept override
{
WlanCloseHandle(m_h, NULL);
}
};
}
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
winstd::handle< HANDLE, NULL >::handle_type
HANDLE handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
winstd::handle< HANDLE, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:855
winstd::handle< HANDLE, NULL >::attach
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
winstd::wlan_handle
WLAN handle wrapper.
Definition: WLAN.h:128
winstd::wlan_handle::~wlan_handle
virtual ~wlan_handle()
Closes a connection to the server.
Definition: WLAN.h:137
winstd::wlan_handle::free_internal
void free_internal() noexcept override
Closes a connection to the server.
Definition: WLAN.h:171
winstd::wlan_handle::open
bool open(DWORD dwClientVersion, PDWORD pdwNegotiatedVersion) noexcept
Opens a connection to the server.
Definition: WLAN.h:152
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:162
winstd::handle< HANDLE, NULL >::invalid
static const HANDLE invalid
Invalid handle value.
Definition: Common.h:614
winstd::WlanFreeMemory_delete< _Ty[]>::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:104
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Other *) const
Delete a pointer of another type.
Definition: WLAN.h:118
winstd::WlanFreeMemory_delete< _Ty[]>::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:99
winstd::WlanFreeMemory_delete< _Ty[]>::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:109
winstd::WlanFreeMemory_delete
Deleter for unique_ptr using WlanFreeMemory.
Definition: WLAN.h:72
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete()
Default construct.
Definition: WLAN.h:78
winstd::WlanFreeMemory_delete::operator()
void operator()(_Ty *_Ptr) const
Delete a pointer.
Definition: WLAN.h:88
winstd::WlanFreeMemory_delete::_Myt
WlanFreeMemory_delete< _Ty > _Myt
This type.
Definition: WLAN.h:73
winstd::WlanFreeMemory_delete::WlanFreeMemory_delete
WlanFreeMemory_delete(const WlanFreeMemory_delete< _Ty2 > &)
Construct from another WlanFreeMemory_delete.
Definition: WLAN.h:83