示例#1
0
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            SSIDText.Text = Settings.SSID;
            KeyText.Text  = Settings.Key;

            WlanMgr = new WlanManager();
            if (!WlanMgr.IsHostedNetworkSupported)
            {
                UnsupportedDevices();
                return;
            }

            if (WlanMgr.IsHostedNetworkActive)
            {
                IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid);
                WlanMgr.ForceStop();
            }

            NetIList.DropDown += UpdateInterfaceList;
            NetIList.SelectionChangeCommitted += OnInterfaceSelected;
            DataGridList.CellContentClick     += OnCellContentClick;
            DataGridList.CellEndEdit          += OnCellEndEdit;
            ShareButton.MouseClick            += OnShareButtonClick;

            WlanMgr.HostedNetworkStateChange      += OnHostedNetworkStateChange;
            WlanMgr.HostedNetworkPeerStateChanged += OnHostedNetworkPeerStateChanged;
            WlanMgr.WlanSoftwareRadioStateChanged += OnWlanSoftwareRadioStateChanged;
            OnHostedNetworkStateChange(WlanMgr.HostedNetworkState);

            System.Threading.Tasks.Task.Run(() => UpdateHostedNetworkStatistic());
            System.Threading.Tasks.Task.Run(() => UpdatePeerListInfo());
        }
示例#2
0
        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;
            }
        }