Esempio n. 1
0
        /// <summary>
        /// Запускает точку доступа с указанным адаптером для ICS
        /// </summary>
        /// <param name="sharedConnectionGuid">адаптер для ICS</param>
        /// <returns>true, если точка доступа успешно запущена; false, если возникла ошибка. Вызовите GetLastError(), чтобы получить результат ошибки</returns>
        public bool Start(SharableConnection sharedConnection)
        {
            try
            {
                Stop();

                _wlanManager.StartHostedNetwork();

                System.Threading.Thread.Sleep(1000);

                if (sharedConnection != null)
                {
                    if (sharedConnection.Guid != Guid.Empty)
                    {
                        if (_icsManager.SharingInstalled)
                        {
                            _icsManager.DisableIcsOnAll();

                            var privateConnectionGuid = _wlanManager.HostedNetworkInterfaceGuid;

                            if (privateConnectionGuid == Guid.Empty)
                            {
                                // If the GUID for the Hosted Network Adapter isn't return properly,
                                // then retrieve it by the DeviceName.

                                privateConnectionGuid = (from c in _icsManager.Connections
                                                         where c.Props.DeviceName.ToLowerInvariant().Contains("microsoft virtual wifi miniport adapter") ||                                                      // Windows 7
                                                         c.Props.DeviceName.ToLowerInvariant().Contains("microsoft hosted network virtual adapter")                                                            // Windows 8
                                                         select c.Guid).FirstOrDefault();
                                // Note: For some reason the DeviceName can have different names, currently it checks for the ones that I have identified thus far.

                                if (privateConnectionGuid == Guid.Empty)
                                {
                                    // Device still now found, so throw exception so the message gets raised up to the client.
                                    throw new Exception("Virtual Wifi device not found!\n\nNeither \"Microsoft Hosted Network Virtual Adapter\" or \"Microsoft Virtual Wifi Miniport Adapter\" were found.");
                                }
                            }

                            _icsManager.EnableIcs(sharedConnection.Guid, privateConnectionGuid);

                            _currentSharedConnection = sharedConnection;
                        }
                    }
                }
                else
                {
                    _currentSharedConnection = null;
                }

                return(true);
            }
            catch (Exception ex)
            {
                _lastErrorMessage = ex.Message;
                return(false);
            }
        }
        private void DisableSharing(bool force = false)
        {
            if (!force)
            {
                if (_sharedConnection == null)
                {
#if TRACE
                    LogExceptions.LogTrace(true, "WLanDisableSharing,NotShared,forced?=" + force);
#endif
                    return;
                }
#if TRACE
                LogExceptions.LogTrace(true, "WLanDisableSharing,Shared,forced?=" + force);
#endif
            }
            else
            {
#if TRACE
                LogExceptions.LogTrace(true, "WLanDisableSharing,NoState,forced?=" + force);
#endif
            }
            if (_icsManager.SharingInstalled)
            {
                _icsManager.DisableIcsOnAll();
            }
            SharedConnection = null;
        }