forked from play175/wifiNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifiNotifer_windows.go
165 lines (128 loc) · 3.94 KB
/
wifiNotifer_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// +build windows
package wifiNotifier
/*
#cgo LDFLAGS: -lwlanapi -lole32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <wlanapi.h>
#include <windot11.h> // for DOT11_SSID struct
#include <objbase.h>
#include <wtypes.h>
//#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
static HANDLE hClient = NULL;
static inline void openHandle()
{
if(hClient != NULL)return;
DWORD dwMaxClient = 2;
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
}
}
static inline void wchar2char(const wchar_t* wchar , char * m_char)
{
int len= WideCharToMultiByte( CP_ACP ,0,wchar ,wcslen( wchar ), NULL,0, NULL ,NULL );
WideCharToMultiByte( CP_ACP ,0,wchar ,wcslen( wchar ),m_char,len, NULL ,NULL );
m_char[len]= '\0';
}
static inline char * getCurrentSSID(void) {
openHandle();
char *ssid = malloc(256);
memset(ssid,0,256);
DWORD dwResult = 0;
unsigned int i;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
} else {
for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO *) & pIfList->InterfaceInfo[i];
if (pIfInfo->isState == wlan_interface_state_connected) {
dwResult = WlanQueryInterface(hClient,
&pIfInfo->InterfaceGuid,
wlan_intf_opcode_current_connection,
NULL,
&connectInfoSize,
(PVOID *) &pConnectInfo,
&opCode);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanQueryInterface failed with error: %u\n", dwResult);
} else {
//wprintf(L" Profile name used:\t %ws\n", pConnectInfo->strProfileName);
wchar2char(pConnectInfo->strProfileName,ssid);
}
}
}
}
if (pConnectInfo != NULL) {
WlanFreeMemory(pConnectInfo);
pConnectInfo = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return ssid;
}
static inline void onWifiChanged(PWLAN_NOTIFICATION_DATA data,PVOID context)
{
extern void __onWifiChanged(char *);
__onWifiChanged(getCurrentSSID());
}
static inline void setWifiNotifier()
{
openHandle();
DWORD hResult = ERROR_SUCCESS;
DWORD pdwPrevNotifSource = 0;
hResult=WlanRegisterNotification(hClient,
WLAN_NOTIFICATION_SOURCE_ACM,
TRUE,
(WLAN_NOTIFICATION_CALLBACK)onWifiChanged,
NULL,
NULL,
&pdwPrevNotifSource);
if(hResult!=ERROR_SUCCESS)
{
printf("failed WlanRegisterNotification=%d \n",hResult);
}
while(TRUE){
Sleep(10);
}
WlanCloseHandle(hClient,NULL);
printf("WlanCloseHandle success \n");
}
*/
import "C"
import "unsafe"
var internalOnWifiChangedCb func(string)
var internalOnGetSSIDCb func(string)
//export __onWifiChanged
func __onWifiChanged(ssid *C.char) {
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
if internalOnWifiChangedCb != nil {
internalOnWifiChangedCb(goSsid)
}
}
func GetCurrentSSID() string {
ssid := C.getCurrentSSID()
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
return goSsid
}
func SetWifiNotifier(cb func(string)) {
internalOnWifiChangedCb = cb
go C.setWifiNotifier()
// log.Println("setWifiNotifier complated")
}