Exemplo n.º 1
0
 /// <summary>
 /// Connects (associates) to the specified wireless network, returning either on a success to connect
 /// or a failure.
 /// </summary>
 /// <param name="connectionMode"></param>
 /// <param name="bssType"></param>
 /// <param name="profile"></param>
 /// <param name="connectTimeout"></param>
 /// <returns></returns>
 public bool ConnectSynchronously(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, string profile, int connectTimeout)
 {
     queueEvents = true;
     try
     {
         Connect(connectionMode, bssType, profile);
         while (queueEvents && eventQueueFilled.WaitOne(connectTimeout, true))
         {
             lock (eventQueue)
             {
                 while (eventQueue.Count != 0)
                 {
                     object e = eventQueue.Dequeue();
                     if (e is WlanConnectionNotificationEventData)
                     {
                         WlanConnectionNotificationEventData wlanConnectionData = (WlanConnectionNotificationEventData)e;
                         // Check if the conditions are good to indicate either success or failure.
                         if (wlanConnectionData.notifyData.notificationSource == Wlan.WlanNotificationSource.ACM)
                         {
                             switch ((Wlan.WlanNotificationCodeAcm)wlanConnectionData.notifyData.notificationCode)
                             {
                             case Wlan.WlanNotificationCodeAcm.ConnectionComplete:
                                 if (wlanConnectionData.connNotifyData.profileName == profile)
                                 {
                                     return(true);
                                 }
                                 break;
                             }
                         }
                         break;
                     }
                 }
             }
         }
     }
     finally
     {
         queueEvents = false;
         eventQueue.Clear();
     }
     return(false);                // timeout expired and no "connection complete"
 }
            public Wlan.WlanBssEntry[] GetNetworkBssList(Wlan.Dot11Ssid ssid, Wlan.Dot11BssType bssType, bool securityEnabled)
            {
                IntPtr num = Marshal.AllocHGlobal(Marshal.SizeOf((object)ssid));

                Marshal.StructureToPtr((object)ssid, num, false);
                try
                {
                    IntPtr wlanBssList;
                    Wlan.ThrowIfError(Wlan.WlanGetNetworkBssList(this.client.clientHandle, this.info.interfaceGuid, num, bssType, securityEnabled, IntPtr.Zero, out wlanBssList));
                    try
                    {
                        return(WlanClient.WlanInterface.ConvertBssListPtr(wlanBssList));
                    }
                    finally
                    {
                        Wlan.WlanFreeMemory(wlanBssList);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(num);
                }
            }
Exemplo n.º 3
0
            /// <summary>
            /// Retrieves the basic service sets (BSS) list of the specified network.
            /// </summary>
            /// <param name="ssid">Specifies the SSID of the network from which the BSS list is requested.</param>
            /// <param name="bssType">Indicates the BSS type of the network.</param>
            /// <param name="securityEnabled">Indicates whether security is enabled on the network.</param>
            public Wlan.WlanBssEntry[] GetNetworkBssList(Wlan.Dot11Ssid ssid, Wlan.Dot11BssType bssType, bool securityEnabled)
            {
                IntPtr ssidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid));

                Marshal.StructureToPtr(ssid, ssidPtr, false);
                try
                {
                    IntPtr bssListPtr;
                    Wlan.ThrowIfError(
                        Wlan.WlanGetNetworkBssList(client.clientHandle, info.interfaceGuid, ssidPtr, bssType, securityEnabled, IntPtr.Zero, out bssListPtr));
                    try
                    {
                        return(ConvertBssListPtr(bssListPtr));
                    }
                    finally
                    {
                        Wlan.WlanFreeMemory(bssListPtr);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(ssidPtr);
                }
            }
Exemplo n.º 4
0
        public void JoinWifi(string ssidToJoin, Wlan.Dot11BssType bssType, BackgroundWorker worker, DoWorkEventArgs e)
        {
            IJcwTaskResult result = new JcwTaskResult();

            result.Status   = TaskResultStatus.Failed;
            Error.LastError = null;

            // If the ssid to join is null, return without attempting to join.
            if (string.IsNullOrEmpty(ssidToJoin))
            {
                return;
            }

            // The SSID profile name for adhoc access points includes the -adhoc suffix.
            string ssidProfileName = ssidToJoin;

            if (bssType == Wlan.Dot11BssType.Independent)
            {
                ssidProfileName += "-adhoc";
            }

            // If we are already connected to that ssid, return without attempting to join. Note that the SSID in the
            // CurrentAccessPoint is actually the SSID profile name.
            if (ssidProfileName.Equals(CurrentAccessPoint.SSID))
            {
                result.Status = TaskResultStatus.Passed;
                result.Text   = string.Format("Already connected to wifi access point: {0}", ssidProfileName);
                e.Result      = result;
                return;
            }

            try
            {
                if (worker != null && worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                // If we are currently connected to a wireless access point other than the one we want to join, disconnect from it.
                if (!Wifi.NotConnected.Equals(CurrentAccessPoint.SSID))
                {
                    DisconnectWifi();
                }

                if (worker != null && worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                // Check to see if the profile exists on this system before trying to connect.
                try
                {
                    string profileXml = WlanInterface.GetProfileXml(ssidProfileName);
                }
                catch (Win32Exception ex)
                {
                    // If the exception is 'Element not found' this profile has not been registered.  If it is an adhoc access point we
                    // should register the profile for it.
                    if ("Element not found".Equals(ex.Message) && bssType == Wlan.Dot11BssType.Independent)
                    {
                        string profileXml = WifiHelper.GetAdhocProfileXmlForSSID(ssidToJoin);
                        Debug.WriteLine(string.Format("Setting wifi profile xml {0}.", profileXml));
                        WlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                    }
                }

                if (worker != null && worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                bool      connectComplete = false;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                try
                {
                    // Create unsignaled manual reset event that will be signaled in the NetworkAddressChanged handler.
                    ConnectionStatusChangedManualResetEvent = new ManualResetEvent(false);

                    // Windows XP with SP3 and Wireless LAN API for Windows XP with SP2:  Only the wlan_connection_mode_profile value is supported.
                    WlanInterface.Connect(Wlan.WlanConnectionMode.Profile, bssType, ssidProfileName);

                    // Subscribe to the NetworkAddressChanged event and wait for either the manual reset event to be signaled or the connection timeout.
                    NetworkChange.NetworkAddressChanged += NetworkAddressChanged_ConnectStatus;

                    int totalWaitTime = 0;
                    int loopWaitTime  = ConnectionTimeout / ConnectionIntervals;
                    do
                    {
                        connectComplete = ConnectionStatusChangedManualResetEvent.WaitOne(loopWaitTime);
                        totalWaitTime  += loopWaitTime;

                        if (worker != null && worker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }
                    }while (!connectComplete && totalWaitTime < ConnectionTimeout);

                    if (connectComplete)
                    {
                        // Create unsignaled manual reset event that will be signaled in the NetworkAddressChanged handler.
                        ActiveConnectionMonitoringManualResetEvent = new ManualResetEvent(false);

                        // If the connection was established successfully, queue a method to the thread pool to monitor this active connection.
                        ThreadPool.QueueUserWorkItem(MonitorActiveWifiConnection);

                        result.Status = TaskResultStatus.Passed;
                        result.Text   = "Joined wifi access point";
                    }
                    else
                    {
                        result.Text = "Timeout attempting to join wifi access point";
                    }
                }
                finally
                {
                    NetworkChange.NetworkAddressChanged -= NetworkAddressChanged_ConnectStatus;
                    sw.Stop();
                    Debug.WriteLine(string.Format("Time attempting to connect: {0}ms", sw.ElapsedMilliseconds));

                    // If the user canceled the connect disconnect before exiting. Use the internal disconnect because
                    // the regular disconnect checks to see if we are connected to the current access point and only
                    // then would it attempt to disconnect.
                    if (e.Cancel)
                    {
                        DisconnectWifiInternal();
                    }
                }
            }
            catch (Win32Exception ex)
            {
                Error.LastError = ex.ToString();
            }
            finally
            {
                e.Result = result;
            }
        }
 public static extern int WlanGetNetworkBssList([In] IntPtr clientHandle, [MarshalAs(UnmanagedType.LPStruct), In] Guid interfaceGuid, [In] IntPtr dot11SsidInt, [In] Wlan.Dot11BssType dot11BssType, [In] bool securityEnabled, IntPtr reservedPtr, out IntPtr wlanBssList);
Exemplo n.º 6
0
 public void Connect(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, Wlan.Dot11Ssid ssid,
                     Wlan.WlanConnectionFlags flags)
 {
     _wlanInterface.Connect(connectionMode, bssType, ssid, flags);
 }
Exemplo n.º 7
0
 public bool ConnectSynchronously(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType,
                                  string profile, int connectTimeout)
 {
     return(_wlanInterface.ConnectSynchronously(connectionMode, bssType, profile, connectTimeout));
 }
Exemplo n.º 8
0
 public void Connect(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, string profile)
 {
     _wlanInterface.Connect(connectionMode, bssType, profile);
 }
Exemplo n.º 9
0
 public Wlan.WlanBssEntry[] GetNetworkBssList(Wlan.Dot11Ssid ssid, Wlan.Dot11BssType bssType,
                                              bool securityEnabled)
 {
     return(_wlanInterface.GetNetworkBssList(ssid, bssType, securityEnabled));
 }