private void OnMainFormClosing(object sender, FormClosingEventArgs e) { if (!(WlanMgr is null)) { _ = IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid); ICSRouter.StopSharing(); _ = WlanMgr.ForceStop(); } }
internal void StartHostedNetwork(string ssid, string key, Guid InterfaceGuid) { WLAN_HOSTED_NETWORK_REASON WlanHnkReason; WlanMgr.QueryConnectionSettings(out string SSID, out uint MaxNumberOfPeers); WlanMgr.SetConnectionSettings(ssid, MaxNumberOfPeers); WlanMgr.SetSecondaryKey(key, true, true); switch (WlanMgr.HostedNetworkState) { case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active: try { IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid); ICSRouter.StopSharing(); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { _ = WlanMgr.ForceStop(); } break; case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_unavailable: if (!(Advapi32.GetPrivileges() && WlanMgr.AllowHostedNetwork(true))) { MessageBox.Show("The hosted network couldn't be started.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); break; } WlanHnkReason = WlanMgr.ForceStart(); if (WlanHnkReason != 0) { MessageBox.Show(ShowHostedNetworkReason(WlanHnkReason), Text); break; } while (WlanHnkReason == 0 && !WlanMgr.IsHostedNetworkActive) { Thread.Sleep(TimeSpan.FromSeconds(1)); } try { ICSRouter.StartSharing(InterfaceGuid, WlanMgr.HostedNetworkGuid); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_idle: WlanHnkReason = WlanMgr.ForceStart(); if (WlanHnkReason != 0) { MessageBox.Show(ShowHostedNetworkReason(WlanHnkReason), Text); break; } while (WlanHnkReason == 0 && !WlanMgr.IsHostedNetworkActive) { Thread.Sleep(TimeSpan.FromSeconds(1)); } try { ICSRouter.StartSharing(InterfaceGuid, WlanMgr.HostedNetworkGuid); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; } }