Example #1
0
        /*
         *      private void DisableSharing()
         *      {
         *          connections = netSharingMgr.EnumEveryConnection;
         *          foreach (INetConnection connection in connections)
         *          {
         *              INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection];
         *              INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection];
         *              try
         *              {
         *                  if (connSharcf.SharingEnabled)
         *                      connSharcf.DisableSharing();
         *              }
         *              catch (Exception e)
         *              {
         *                  MessageBox.Show(e.Message);
         *              }
         *          }
         *      }
         */

        public bool EnableSharing(NetConnection nc)
        {
            if (nc == null)
            {
                return(false);
            }
            try
            {
                netSharingMgr = new NetSharingManagerClass();
                connections   = netSharingMgr.EnumEveryConnection;
                foreach (INetConnection connection in connections)
                {
                    INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection];
                    INetConnectionProps      connProps  = netSharingMgr.NetConnectionProps[connection];
                    try
                    {
                        //判断要配置的网卡 Microsoft Virtual WiFi Miniport Adapter   Realtek PCIe GBE Family Controller

                        if (connProps.DeviceName.Equals(nc.DeviceName))
                        {
                            //配置WAN连接
                            if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC)
                            {
                                connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                            }
                        }
                        else if (connProps.DeviceName.Contains("Virtual"))
                        {
                            //配置LAN连接
                            if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC)
                            {
                                connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手");
                        return(false);
                    }
                }
                SharedConnection = nc;
            }
            catch (Exception ex)
            {
                MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手");
                return(false);
            }
            return(true);
        }
Example #2
0
        public bool Start()
        {
            try
            {
                INetConnection dialer = GetDialerConnection();
                INetConnection wifi   = GetAPConnection();
                if (dialer == null || wifi == null)
                {
                    return(false);
                }

                INetSharingConfiguration wconf = nsm.INetSharingConfigurationForINetConnection[wifi];
                Disable_ICS_WMI(false);
                if (wconf.SharingEnabled)
                {
                    wconf.DisableSharing();
                }
                wconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);

                INetSharingConfiguration dconf = nsm.INetSharingConfigurationForINetConnection[dialer];
                Disable_ICS_WMI(true);
                if (dconf.SharingEnabled)
                {
                    dconf.DisableSharing();
                }
                // System.AccessViolationException???
                dconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
            }
            catch (Exception e)
            {
                Log4Net.WriteLog("Enable sharing wifi failed", e);
                return(false);
            }
            return(true);
        }
Example #3
0
        public static void StartSharing(Guid InterfaceGuid, tagSHARINGCONNECTIONTYPE ShConType)
        {
            foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection)
            {
                INetConnectionProps      INetConProps = INetSharingMgr.NetConnectionProps[NetConnection];
                INetSharingConfiguration INetShConfig = INetSharingMgr.INetSharingConfigurationForINetConnection[NetConnection];

                if (!Guid.Parse(INetConProps.Guid).Equals(InterfaceGuid))
                {
                    continue;
                }
                switch (ShConType)
                {
                case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC:
                    INetShConfig.EnableSharing(ShConType);
                    Trace.WriteLine(
                        $"\n" +
                        $"Start ICS Type: Public" +
                        $"\nName: {INetConProps.Name}" +
                        $"\nDeviceName: {INetConProps.DeviceName}" +
                        $"\nGuid: {INetConProps.Guid}" +
                        $"\n");
                    break;

                case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE:
                    INetShConfig.EnableSharing(ShConType);
                    Trace.WriteLine(
                        $"\n" +
                        $"Start ICS Type: Private" +
                        $"\nName: {INetConProps.Name}" +
                        $"\nDeviceName: {INetConProps.DeviceName}" +
                        $"\nGuid: {INetConProps.Guid}" +
                        $"\n");
                    break;
                }
            }
        }
Example #4
0
        private void AddPrivConn(object sender, EventArgs e)
        {
            try {
                NetSharingManager manager = new NetSharingManagerClass();
                INetConnection    con     = inet_connections[priv_con_cb_index[cb_con_priv.SelectedIndex]];

                INetConnectionProps      prop = manager.NetConnectionProps[con];
                INetSharingConfiguration conf = manager.INetSharingConfigurationForINetConnection[con];

                conf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                priv_con.Add(prop.DeviceName);
            } catch (Exception) {
            }

            UpdateICS();
        }
        public bool JShareWIFI(bool isShare, out string jShareWIFIRet)
        {
            jShareWIFIRet = "未找到本地网络连接!";
            if (!isShare)
            {
                if (sharingCfg != null)
                {
                    sharingCfg = null;
                }
                return(true);
            }
            try
            {
                string connectionToShare   = getBestInterface()?.Name; // 被共享的网络连接
                string sharedForConnection = "";                       // 共享的家庭网络连接

                var manager     = new NetSharingManager();
                var connections = manager.EnumEveryConnection;

                NetworkInterface[] Ninterface = NetworkInterface.GetAllNetworkInterfaces();//确定虚拟网络名称
                foreach (NetworkInterface IN in Ninterface)
                {
                    if (IN.Description.Contains("Microsoft Hosted Network Virtual Adapter") && IN.OperationalStatus == OperationalStatus.Up)
                    {
                        sharedForConnection = IN.Name;
                        break;
                    }
                }

                if (string.IsNullOrWhiteSpace(connectionToShare) || string.IsNullOrWhiteSpace(sharedForConnection))
                {
                    return(false);
                }

                foreach (INetConnection c in connections)
                {
                    var props = manager.NetConnectionProps[c];
                    INetSharingConfiguration tempSharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name == connectionToShare)
                    {
                        tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                        if (tempSharingCfg.SharingEnabled == true)
                        {
                            jShareWIFIRet = "已设置" + props.Name + "用于共享";
                            sharingCfg    = tempSharingCfg;
                        }
                    }
                    else if (props.Name == sharedForConnection)
                    {
                        tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                        priSharingCfg = tempSharingCfg;
                    }
                }


                jShareWIFIRet = "Success";
                return(true);
            }
            catch (Exception e)
            {
                jShareWIFIRet = e.Message;
                return(false);
            }
        }