private bool StartListening() { try { var result = WlanApi.WlanOpenHandle(2, IntPtr.Zero, out uint currentVersion, out clientHandle); if (result != 0) { return(false); } wlanNotifyDelegate = new WlanNotificationCallback(WlanNotifyHook); return(WlanRegisterNotification(clientHandle, WlanApi.WlanNotificationSource.MSM, true, wlanNotifyDelegate, IntPtr.Zero, IntPtr.Zero, out WlanApi.WlanNotificationSource prevSource) == 0); } catch (System.DllNotFoundException e) { ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error); } return(false); }
private void button_Start_Click(object sender, EventArgs e) { _WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS WHNCS = new _WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS { hostedNetworkSSID = WlanApi.Create_DOT11_SSID(textBox_SSID.Text), dwMaxNumberOfPeers = 1 }; WlanApi.WlanOpenHandle(2, IntPtr.Zero, out CurVerison, out WlanHANDLE); WlanApi.WlanHostedNetworkInitSettings(WlanHANDLE, out FailReason, IntPtr.Zero); IntPtr data = Marshal.AllocHGlobal(Marshal.SizeOf(WHNCS)); Marshal.StructureToPtr(WHNCS, data, false); WlanApi.WlanHostedNetworkSetProperty(WlanHANDLE, _WLAN_HOSTED_NETWORK_OPCODE.wlan_hosted_network_opcode_connection_settings, (uint)Marshal.SizeOf(WHNCS), data, out FailReason, IntPtr.Zero); byte[] B = Encoding.Default.GetBytes(textBox_PassWord.Text); IntPtr key_Ptr = Marshal.AllocHGlobal(B.Length + 1); for (int c = 0; c < B.Length; c++) { Marshal.WriteByte(key_Ptr, c, B[c]); } Marshal.WriteByte(key_Ptr, B.Length, 0); WlanApi.WlanHostedNetworkSetSecondaryKey(WlanHANDLE, (uint)B.Length + 1, key_Ptr, true, false, out FailReason, IntPtr.Zero ); WlanApi.WlanHostedNetworkForceStart(WlanHANDLE, out FailReason, IntPtr.Zero); WlanApi.WlanCloseHandle(WlanHANDLE, IntPtr.Zero); }
private void button_Stop_Click(object sender, EventArgs e) { WlanApi.WlanOpenHandle(2, IntPtr.Zero, out CurVerison, out WlanHANDLE); WlanApi.WlanHostedNetworkForceStop(WlanHANDLE, out FailReason, IntPtr.Zero); WlanApi.WlanCloseHandle(WlanHANDLE, IntPtr.Zero); }
/// <summary> /// Callback function for reacting to changes in wlan connectivity. /// </summary> /// <param name="notificationData">WLAN notification data structure containing details about the notification.</param> /// <param name="context">Notification context, not used.</param> public void WlanNotifyHook(ref WlanApi.WlanNotificationData notificationData, IntPtr context) { // Do not perform wlan detection if user's unsecure network alert setting is turned off. if (!Manager.Settings.Network.UnsecureNetworkAlert) { return; } // If connected, ignore any and all messages if (Manager.MainWindowViewModel.Status == Models.ConnectionState.Protected) { return; } const int clientVersion = 2; if ((WlanApi.WlanNotificationMsm)notificationData.NotificationCode == WlanApi.WlanNotificationMsm.Connected) { if (WlanApi.WlanOpenHandle(clientVersion, IntPtr.Zero, out uint _, out IntPtr clientHandle) != 0) { ErrorHandling.ErrorHandler.Handle("Can't open Wlan handle for unsecured network detection.", ErrorHandling.LogLevel.Error); return; } var queryData = IntPtr.Zero; var wlanQueryResult = WlanApi.WlanQueryInterface(clientHandle, new Guid(notificationData.InterfaceGuid.ToString()), WlanApi.WlanIntfOpcode.CurrentConnection, IntPtr.Zero, out _, ref queryData, IntPtr.Zero); if (wlanQueryResult == 0) { var connectionData = Marshal.PtrToStructure <WlanApi.WlanConnectionAttributes>(queryData); // Detect open network or WEP secured network if (connectionData.WlanSecurityAttributes.Dot11AuthAlgorithm == WlanApi.Dot11AuthAlgorithm.Open || ( connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep || connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep40 || connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep104) ) { var showNotification = true; var bSsid = connectionData.WlanAssociationAttributes.Dot11Bssid.ToString(); if (!accessPoints.ContainsKey(connectionData.WlanAssociationAttributes.Dot11Bssid.ToString())) { var newAp = new AccessPoint() { Ssid = connectionData.ProfileName, Bssid = bSsid, LastNotified = DateTime.UtcNow, }; accessPoints[bSsid] = newAp; } else { if ((DateTime.UtcNow - accessPoints[bSsid].LastNotified).TotalSeconds < ProductConstants.InsecureWiFiTimeout) { showNotification = false; } var accessPoint = accessPoints[bSsid]; accessPoint.LastNotified = DateTime.UtcNow; accessPoints[bSsid] = accessPoint; } if (showNotification) { Manager.TrayIcon.ShowNotification( Manager.TranslationService.GetString("wifi-unsecure-network-detected-msg"), string.Concat(Manager.TranslationService.GetString("wifi-network-unsecure-msg", UI.Resources.Localization.TranslationService.Args("wifiName", connectionData.WlanAssociationAttributes.Dot11Ssid.ToString())), " ", Manager.TranslationService.GetString("wifi-you-should-turn-on-msg")), NotificationArea.ToastIconType.Disconnected ); } } if (queryData != IntPtr.Zero) { WlanApi.WlanFreeMemory(queryData); } } } }
public WlanClient() { Wlan.ThrowIfError( WlanApi.WlanOpenHandle( Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle)); }