From 276546ee18b982be584099b79440aa5af7b26cbb Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Sun, 28 Aug 2016 12:21:03 +0200 Subject: [PATCH] Additional support for working with WLAN added --- build/WinStd.vcxproj | 1 + build/WinStd.vcxproj.filters | 3 + include/WinStd/WLAN.h | 151 +++++++++++++++++++++++++++++++++++ src/Sec.cpp | 1 + src/WLAN.cpp | 40 ++++++++++ 5 files changed, 196 insertions(+) create mode 100644 src/WLAN.cpp diff --git a/build/WinStd.vcxproj b/build/WinStd.vcxproj index 03fa496d..5417fcc4 100644 --- a/build/WinStd.vcxproj +++ b/build/WinStd.vcxproj @@ -85,6 +85,7 @@ Create + diff --git a/build/WinStd.vcxproj.filters b/build/WinStd.vcxproj.filters index 3305e639..363b635c 100644 --- a/build/WinStd.vcxproj.filters +++ b/build/WinStd.vcxproj.filters @@ -35,6 +35,9 @@ Source Files + + Source Files + diff --git a/include/WinStd/WLAN.h b/include/WinStd/WLAN.h index 8fa18f12..6920323f 100644 --- a/include/WinStd/WLAN.h +++ b/include/WinStd/WLAN.h @@ -34,6 +34,23 @@ extern DWORD (WINAPI *pfnWlanReasonCodeToString)(__in DWORD dwReasonCode, __in D /// /// @{ +namespace winstd { + /// + /// Deleter for unique_ptr using WlanFreeMemory + /// + template struct WlanFreeMemory_delete; + + /// + /// Deleter for unique_ptr to array of unknown size using WlanFreeMemory + /// + template struct WlanFreeMemory_delete<_Ty[]>; + + /// + /// WLAN handle wrapper + /// + class WINSTD_API wlan_handle; +} + /// /// Retrieves a string that describes a specified reason code and stores it in a std::wstring string. /// @@ -50,6 +67,140 @@ template inline DWORD WlanReasonCodeToStr #pragma once +namespace winstd +{ + template struct WlanFreeMemory_delete + { + typedef WlanFreeMemory_delete<_Ty> _Myt; ///< This type + + /// + /// Default construct + /// + WlanFreeMemory_delete() {} + + /// + /// Construct from another WlanFreeMemory_delete + /// + template WlanFreeMemory_delete(const WlanFreeMemory_delete<_Ty2>&) {} + + /// + /// Delete a pointer + /// + void operator()(_Ty *_Ptr) const + { + WlanFreeMemory(_Ptr); + } + }; + + + template struct WlanFreeMemory_delete<_Ty[]> + { + typedef WlanFreeMemory_delete<_Ty> _Myt; ///< This type + + /// + /// Default construct + /// + WlanFreeMemory_delete() {} + + /// + /// Delete a pointer + /// + void operator()(_Ty *_Ptr) const + { + WlanFreeMemory(_Ptr); + } + + /// + /// Delete a pointer of another type + /// + template + void operator()(_Other *) const + { + WlanFreeMemory(_Ptr); + } + }; + + + class WINSTD_API wlan_handle : public handle + { + public: + /// + /// Initializes a new class instance with the object handle set to NULL. + /// + inline wlan_handle() : handle() {} + + /// + /// Initializes a new class instance with an already available object handle. + /// + /// \param[in] h Initial object handle value + /// + inline wlan_handle(_In_opt_ handle_type h) : handle(h) {} + + /// + /// Move constructor + /// + /// \param[inout] h A rvalue reference of another object + /// + inline wlan_handle(_Inout_ wlan_handle &&h) : handle(std::move(h)) {} + + /// + /// Closes a connection to the server. + /// + /// \sa [WlanCloseHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706610(v=vs.85).aspx) + /// + virtual ~wlan_handle(); + + /// + /// Move assignment + /// + /// \param[inout] h A rvalue reference of another object + /// + wlan_handle& operator=(_Inout_ wlan_handle &&h) + { + if (this != std::addressof(h)) + *(handle*)this = std::move(h); + return *this; + } + + /// + /// Opens a connection to the server. + /// + /// \sa [WlanOpenHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706759.aspx) + /// + /// \return + /// - \c true when succeeds; + /// - \c false when fails. Use `GetLastError()` for failure reason. + /// + inline bool open(_In_ DWORD dwClientVersion, _Out_ PDWORD pdwNegotiatedVersion) + { + handle_type h; + DWORD dwResult = WlanOpenHandle(dwClientVersion, 0, pdwNegotiatedVersion, &h); + if (dwResult == ERROR_SUCCESS) { + attach(h); + return true; + } else { + SetLastError(dwResult); + return false; + } + } + + + private: + // This class is noncopyable. + wlan_handle(_In_ const wlan_handle &h); + wlan_handle& operator=(_In_ const wlan_handle &h); + + protected: + /// + /// Closes a connection to the server. + /// + /// \sa [WlanCloseHandle function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706610(v=vs.85).aspx) + /// + virtual void free_internal(); + }; +} + + template inline DWORD WlanReasonCodeToString(_In_ DWORD dwReasonCode, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sValue, __reserved PVOID pReserved) { diff --git a/src/Sec.cpp b/src/Sec.cpp index 43bd2981..656f5721 100644 --- a/src/Sec.cpp +++ b/src/Sec.cpp @@ -20,6 +20,7 @@ #include "StdAfx.h" + #if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC) ////////////////////////////////////////////////////////////////////// diff --git a/src/WLAN.cpp b/src/WLAN.cpp new file mode 100644 index 00000000..d706cc9c --- /dev/null +++ b/src/WLAN.cpp @@ -0,0 +1,40 @@ +/* + Copyright 1991-2016 Amebis + Copyright 2016 GÉANT + + This file is part of WinStd. + + Setup is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Setup is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Setup. If not, see . +*/ + +#include "StdAfx.h" + + +////////////////////////////////////////////////////////////////////// +// winstd::wlan_handle +////////////////////////////////////////////////////////////////////// + +winstd::wlan_handle::~wlan_handle() +{ + if (m_h) + WlanCloseHandle(m_h, NULL); +} + + +void winstd::wlan_handle::free_internal() +{ + WlanCloseHandle(m_h, NULL); +} + +