G

Untitled

public
Guest Jun 11, 2024 Never 43
Clone
C# 1 261 lines (222 loc) | 8.51 KB
1
public enum DOT11_PHY_TYPE : uint
2
{
3
dot11_phy_type_unknown = 0,
4
dot11_phy_type_any = 1,
5
dot11_phy_type_fhss = 2,
6
dot11_phy_type_dsss = 3,
7
dot11_phy_type_irbaseband = 4,
8
dot11_phy_type_ofdm = 5,
9
dot11_phy_type_hrdsss = 6,
10
dot11_phy_type_erp = 7,
11
dot11_phy_type_ht = 8,
12
dot11_phy_type_vht = 9,
13
dot11_phy_type_dmg = 10,
14
dot11_phy_type_he = 11,
15
dot11_phy_type_IHV_start = 0x80000000,
16
dot11_phy_type_IHV_end = 0xffffffff
17
}
18
19
[DllImport("Wlanapi.dll")]
20
public static extern int WlanOpenHandle(
21
uint dwClientVersion,
22
IntPtr pReserved,
23
out uint pdwNegotiatedVersion,
24
out IntPtr phClientHandle);
25
26
[DllImport("Wlanapi.dll")]
27
public static extern int WlanCloseHandle(
28
IntPtr hClientHandle,
29
IntPtr pReserved);
30
31
[DllImport("Wlanapi.dll")]
32
public static extern int WlanEnumInterfaces(
33
IntPtr hClientHandle,
34
IntPtr pReserved,
35
out IntPtr ppInterfaceList);
36
37
[DllImport("Wlanapi.dll")]
38
public static extern void WlanFreeMemory(IntPtr pMemory);
39
40
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
41
public struct WLAN_INTERFACE_INFO_LIST
42
{
43
public uint dwNumberOfItems;
44
public uint dwIndex;
45
public WLAN_INTERFACE_INFO[] InterfaceInfo;
46
47
public WLAN_INTERFACE_INFO_LIST(IntPtr ppInterfaceList)
48
{
49
dwNumberOfItems = (uint)Marshal.ReadInt32(ppInterfaceList, 0);
50
dwIndex = (uint)Marshal.ReadInt32(ppInterfaceList, 4);
51
InterfaceInfo = new WLAN_INTERFACE_INFO[dwNumberOfItems];
52
53
var interfaceList = new IntPtr(ppInterfaceList.ToInt64() + 8);
54
for (int i = 0; i < dwNumberOfItems; i++)
55
{
56
InterfaceInfo[i] = (WLAN_INTERFACE_INFO)Marshal.PtrToStructure(interfaceList, typeof(WLAN_INTERFACE_INFO));
57
interfaceList = new IntPtr(interfaceList.ToInt64() + Marshal.SizeOf(typeof(WLAN_INTERFACE_INFO)));
58
}
59
}
60
}
61
62
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
63
public struct WLAN_INTERFACE_INFO
64
{
65
public Guid InterfaceGuid;
66
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
67
public string strInterfaceDescription;
68
public WLAN_INTERFACE_STATE isState;
69
}
70
71
public enum WLAN_INTERFACE_STATE
72
{
73
wlan_interface_state_not_ready = 0,
74
wlan_interface_state_connected = 1,
75
wlan_interface_state_ad_hoc_network_formed = 2,
76
wlan_interface_state_disconnecting = 3,
77
wlan_interface_state_disconnected = 4,
78
wlan_interface_state_associating = 5,
79
wlan_interface_state_discovering = 6,
80
wlan_interface_state_authenticating = 7
81
}
82
83
[DllImport("Wlanapi.dll")]
84
public static extern int WlanQueryInterface(
85
IntPtr hClientHandle,
86
[MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
87
WLAN_INTF_OPCODE OpCode,
88
IntPtr pReserved,
89
out int pdwDataSize,
90
out IntPtr ppData,
91
IntPtr pWlanOpcodeValueType);
92
93
public enum WLAN_INTF_OPCODE
94
{
95
wlan_intf_opcode_autoconf_start = 0,
96
wlan_intf_opcode_autoconf_enabled,
97
wlan_intf_opcode_background_scan_enabled,
98
wlan_intf_opcode_media_streaming_mode,
99
wlan_intf_opcode_radio_state,
100
wlan_intf_opcode_bss_type,
101
wlan_intf_opcode_interface_state,
102
wlan_intf_opcode_current_connection,
103
wlan_intf_opcode_channel_number,
104
wlan_intf_opcode_supported_infrastructure_auth_cipher_pairs,
105
wlan_intf_opcode_supported_adhoc_auth_cipher_pairs,
106
wlan_intf_opcode_supported_country_or_region_string_list,
107
wlan_intf_opcode_current_operation_mode,
108
wlan_intf_opcode_supported_safe_mode,
109
wlan_intf_opcode_certified_safe_mode,
110
wlan_intf_opcode_hardware_version,
111
wlan_intf_opcode_connected_wifi_vendor_extended_ihv_info,
112
wlan_intf_opcode_supported_wifi_vendor_extended_ihv_info,
113
wlan_intf_opcode_network_filter_list
114
}
115
116
[StructLayout(LayoutKind.Sequential)]
117
public struct WLAN_ASSOCIATION_ATTRIBUTES
118
{
119
public DOT11_SSID dot11Ssid;
120
public uint dot11BssType;
121
public DOT11_MAC_ADDRESS dot11Bssid;
122
public DOT11_PHY_TYPE dot11PhyType;
123
public uint uDot11PhyIndex;
124
public uint wlanSignalQuality;
125
public uint ulRxRate;
126
public uint ulTxRate;
127
}
128
129
[StructLayout(LayoutKind.Sequential)]
130
public struct DOT11_SSID
131
{
132
public uint uSSIDLength;
133
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
134
public byte[] ucSSID;
135
}
136
137
[StructLayout(LayoutKind.Sequential)]
138
public struct DOT11_MAC_ADDRESS
139
{
140
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
141
public byte[] ucDot11MacAddress;
142
}
143
144
int htValue = (int)DOT11_PHY_TYPE.dot11_phy_type_ht;
145
146
147
148
149
public void MyMethod()
150
{
151
// Использование элементов перечисления
152
DOT11_PHY_TYPE phyType = DOT11_PHY_TYPE.dot11_phy_type_ht;
153
154
// Пример сравнения значений перечисления
155
if ((int)phyType == (int)DOT11_PHY_TYPE.dot11_phy_type_ht)
156
{
157
Console.WriteLine("PHY Type is HT");
158
}
159
}
160
161
162
163
private void Timer_Tick(object sender, EventArgs e)
164
{
165
try
166
{
167
// Проверка и запуск службы WLAN AutoConfig, если она не запущена
168
StartWlanService();
169
170
int signalStrength = GetSignalStrength();
171
SetSignalStrength(signalStrength);
172
173
// Если всё прошло хорошо, обновляем состояние программы
174
UpdateProgramStatus(true);
175
}
176
catch (Exception ex)
177
{
178
// В случае ошибки обновляем состояние программы и выводим ошибку в консоль
179
UpdateProgramStatus(false);
180
Console.WriteLine("Exception: " + ex.Message);
181
}
182
}
183
184
185
private int GetSignalStrength()
186
{
187
// Проверка, активна ли служба WLAN AutoConfig
188
ServiceController sc = new ServiceController("WlanSvc");
189
if (sc.Status != ServiceControllerStatus.Running)
190
{
191
throw new Exception("WLAN AutoConfig service is not running.");
192
}
193
194
IntPtr clientHandle;
195
uint negotiatedVersion;
196
197
int result = WlanOpenHandle(2, IntPtr.Zero, out negotiatedVersion, out clientHandle);
198
if (result != 0)
199
{
200
throw new Exception("WlanOpenHandle failed with error code " + result);
201
}
202
203
IntPtr interfaceList;
204
result = WlanEnumInterfaces(clientHandle, IntPtr.Zero, out interfaceList);
205
if (result != 0)
206
{
207
WlanCloseHandle(clientHandle, IntPtr.Zero);
208
throw new Exception("WlanEnumInterfaces failed with error code " + result);
209
}
210
211
WLAN_INTERFACE_INFO_LIST infoList = new WLAN_INTERFACE_INFO_LIST(interfaceList);
212
213
// Проверка, содержит ли массив InterfaceInfo хотя бы один элемент
214
if (infoList.InterfaceInfo.Length > 0)
215
{
216
IntPtr pData;
217
int dataSize;
218
219
result = WlanQueryInterface(clientHandle, infoList.InterfaceInfo[0].InterfaceGuid, WLAN_INTF_OPCODE.wlan_intf_opcode_current_connection, IntPtr.Zero, out dataSize, out pData, IntPtr.Zero);
220
if (result != 0)
221
{
222
WlanFreeMemory(interfaceList);
223
WlanCloseHandle(clientHandle, IntPtr.Zero);
224
throw new Exception("WlanQueryInterface failed with error code " + result);
225
}
226
227
WLAN_ASSOCIATION_ATTRIBUTES attributes = (WLAN_ASSOCIATION_ATTRIBUTES)Marshal.PtrToStructure(pData, typeof(WLAN_ASSOCIATION_ATTRIBUTES));
228
229
WlanFreeMemory(pData);
230
WlanFreeMemory(interfaceList);
231
WlanCloseHandle(clientHandle, IntPtr.Zero);
232
233
return (int)attributes.wlanSignalQuality;
234
}
235
else
236
{
237
// Обработка случая, когда InterfaceInfo пустой
238
throw new Exception("No WLAN interfaces found.");
239
}
240
}
241
242
243
private void SetSignalStrength(int strength)
244
{
245
// Предполагается, что у вас есть элементы управления с именами bar1, bar2 и так далее
246
bar1.Fill = strength >= 20 ? Brushes.Green : Brushes.Gray;
247
bar2.Fill = strength >= 40 ? Brushes.Green : Brushes.Gray;
248
bar3.Fill = strength >= 60 ? Brushes.Green : Brushes.Gray;
249
bar4.Fill = strength >= 80 ? Brushes.Green : Brushes.Gray;
250
bar5.Fill = strength >= 100 ? Brushes.Green : Brushes.Gray;
251
}
252
253
private void StartWlanService()
254
{
255
ServiceController sc = new ServiceController("WlanSvc");
256
if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.Paused)
257
{
258
sc.Start();
259
sc.WaitForStatus(ServiceControllerStatus.Running);
260
}
261
}