Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "win_wlanLibrary.h"
6
7
// Moz headers (alphabetical)
8
9
WinWLANLibrary* WinWLANLibrary::Load() {
10
WinWLANLibrary* ret = new WinWLANLibrary();
11
if (!ret) {
12
return nullptr;
13
}
14
15
if (!ret->Initialize()) {
16
delete ret;
17
return nullptr;
18
}
19
20
return ret;
21
}
22
23
WinWLANLibrary::WinWLANLibrary()
24
: mWlanLibrary(nullptr),
25
mWlanHandle(nullptr),
26
mWlanEnumInterfacesPtr(nullptr),
27
mWlanGetNetworkBssListPtr(nullptr),
28
mWlanFreeMemoryPtr(nullptr),
29
mWlanCloseHandlePtr(nullptr),
30
mWlanOpenHandlePtr(nullptr),
31
mWlanRegisterNotificationPtr(nullptr),
32
mWlanScanPtr(nullptr) {}
33
34
HANDLE
35
WinWLANLibrary::GetWLANHandle() const { return mWlanHandle; }
36
37
decltype(::WlanEnumInterfaces)* WinWLANLibrary::GetWlanEnumInterfacesPtr()
38
const {
39
return mWlanEnumInterfacesPtr;
40
}
41
42
decltype(::WlanGetNetworkBssList)* WinWLANLibrary::GetWlanGetNetworkBssListPtr()
43
const {
44
return mWlanGetNetworkBssListPtr;
45
}
46
47
decltype(::WlanFreeMemory)* WinWLANLibrary::GetWlanFreeMemoryPtr() const {
48
return mWlanFreeMemoryPtr;
49
}
50
51
decltype(::WlanCloseHandle)* WinWLANLibrary::GetWlanCloseHandlePtr() const {
52
return mWlanCloseHandlePtr;
53
}
54
55
decltype(::WlanOpenHandle)* WinWLANLibrary::GetWlanOpenHandlePtr() const {
56
return mWlanOpenHandlePtr;
57
}
58
59
decltype(::WlanRegisterNotification)*
60
WinWLANLibrary::GetWlanRegisterNotificationPtr() const {
61
return mWlanRegisterNotificationPtr;
62
}
63
64
decltype(::WlanScan)* WinWLANLibrary::GetWlanScanPtr() const {
65
return mWlanScanPtr;
66
}
67
68
bool WinWLANLibrary::Initialize() {
69
mWlanLibrary = LoadLibrary("Wlanapi.dll");
70
if (!mWlanLibrary) {
71
return false;
72
}
73
74
mWlanOpenHandlePtr = (decltype(::WlanOpenHandle)*)GetProcAddress(
75
mWlanLibrary, "WlanOpenHandle");
76
mWlanEnumInterfacesPtr = (decltype(::WlanEnumInterfaces)*)GetProcAddress(
77
mWlanLibrary, "WlanEnumInterfaces");
78
mWlanRegisterNotificationPtr =
79
(decltype(::WlanRegisterNotification)*)GetProcAddress(
80
mWlanLibrary, "WlanRegisterNotification");
81
mWlanScanPtr =
82
(decltype(::WlanScan)*)GetProcAddress(mWlanLibrary, "WlanScan");
83
84
mWlanFreeMemoryPtr = (decltype(::WlanFreeMemory)*)GetProcAddress(
85
mWlanLibrary, "WlanFreeMemory");
86
mWlanCloseHandlePtr = (decltype(::WlanCloseHandle)*)GetProcAddress(
87
mWlanLibrary, "WlanCloseHandle");
88
mWlanGetNetworkBssListPtr =
89
(decltype(::WlanGetNetworkBssList)*)GetProcAddress(
90
mWlanLibrary, "WlanGetNetworkBssList");
91
92
if (!mWlanOpenHandlePtr || !mWlanEnumInterfacesPtr ||
93
!mWlanRegisterNotificationPtr || !mWlanGetNetworkBssListPtr ||
94
!mWlanScanPtr || !mWlanFreeMemoryPtr || !mWlanCloseHandlePtr) {
95
return false;
96
}
97
98
// Get the handle to the WLAN API.
99
DWORD negotiated_version;
100
// We could be executing on either Windows XP or Windows Vista, so use the
101
// lower version of the client WLAN API. It seems that the negotiated version
102
// is the Vista version irrespective of what we pass!
103
static const int kXpWlanClientVersion = 1;
104
if (ERROR_SUCCESS != (*mWlanOpenHandlePtr)(kXpWlanClientVersion, nullptr,
105
&negotiated_version,
106
&mWlanHandle)) {
107
return false;
108
}
109
110
return true;
111
}
112
113
WinWLANLibrary::~WinWLANLibrary() {
114
if (mWlanLibrary) {
115
if (mWlanHandle) {
116
(*mWlanCloseHandlePtr)(mWlanLibrary, mWlanHandle);
117
mWlanHandle = nullptr;
118
}
119
::FreeLibrary(mWlanLibrary);
120
mWlanLibrary = nullptr;
121
}
122
}