Example #1
2
 /// <summary>
 /// Requests a connection (association) to the specified wireless network.
 /// </summary>
 /// <remarks>
 /// The method returns immediately. Progress is reported through the <see cref="WlanNotification"/> event.
 /// </remarks>
 public void Connect(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, string profile)
 {
     Wlan.WlanConnectionParameters connectionParams = new Wlan.WlanConnectionParameters();
     connectionParams.wlanConnectionMode = connectionMode;
     connectionParams.profile = profile;
     connectionParams.dot11BssType = bssType;
     connectionParams.flags = 0;
     Connect(connectionParams);
 }
Example #2
1
 /// <summary>
 /// Connects to the specified wireless network.
 /// </summary>
 /// <remarks>
 /// The method returns immediately. Progress is reported through the <see cref="WlanNotification"/> event.
 /// </remarks>
 public void Connect(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, Wlan.Dot11Ssid ssid, Wlan.WlanConnectionFlags flags)
 {
     Wlan.WlanConnectionParameters connectionParams = new Wlan.WlanConnectionParameters();
     connectionParams.wlanConnectionMode = connectionMode;
     connectionParams.dot11SsidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid));
     Marshal.StructureToPtr(ssid, connectionParams.dot11SsidPtr, false);
     connectionParams.dot11BssType = bssType;
     connectionParams.flags = flags;
     Connect(connectionParams);
     Marshal.DestroyStructure(connectionParams.dot11SsidPtr, ssid.GetType());
     Marshal.FreeHGlobal(connectionParams.dot11SsidPtr);
 }
        //Button - Confirm
        private void btnConfirm_MouseUp(object sender, MouseEventArgs e)
        {
            //string profileXml = string.Format("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><hex>{1}</hex><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
            uint pdwNegotiatedVersion;
            IntPtr phClientHandle;
            IntPtr pReserved = IntPtr.Zero;

            Wlan.WlanConnectionParameters ConParams = new Wlan.WlanConnectionParameters();
            ConParams.wlanConnectionMode = Wlan.WlanConnectionMode.Profile;
            ConParams.profile = SelectedNetwork.ProfileName;
            IntPtr dot11SsidPtr = Marshal.AllocHGlobal((int)SelectedNetwork.dot11Ssid.SSIDLength);
            Marshal.StructureToPtr(SelectedNetwork.dot11Ssid,dot11SsidPtr,false);
            ConParams.dot11SsidPtr = dot11SsidPtr;
            IntPtr desiredBssidListPtr;
            Wlan.WlanOpenHandle(2, pReserved, out pdwNegotiatedVersion, out phClientHandle);
            Wlan.WlanGetNetworkBssList(phClientHandle,SelectedInterface.InterfaceGuid,dot11SsidPtr,SelectedNetwork.dot11BssType, true,pReserved, out desiredBssidListPtr);
            ConParams.desiredBssidListPtr = desiredBssidListPtr;
            ConParams.dot11BssType = SelectedNetwork.dot11BssType;
            ConParams.flags = Wlan.WlanConnectionFlags.HiddenNetwork;

            string DefaultAuthAlgorithm = "";
            string DefaultCipherAlgorithm = "";

            switch (SelectedNetwork.dot11DefaultAuthAlgorithm)
            {
                case Wlan.Dot11AuthAlgorithm.RSNA_PSK:
                    DefaultAuthAlgorithm = "WPA2PSK";
                    break;
                case Wlan.Dot11AuthAlgorithm.WPA_PSK:
                    DefaultAuthAlgorithm = "WPAPSK";
                    break;
                default:
                    break;
            }

            switch (SelectedNetwork.dot11DefaultCipherAlgorithm)
            {
                case Wlan.Dot11CipherAlgorithm.CCMP:
                    DefaultCipherAlgorithm = "AES";
                    break;
                case Wlan.Dot11CipherAlgorithm.TKIP:
                    DefaultCipherAlgorithm = "TKIP";
                    break;
                default:
                    break;
            }

            string profileXml = string.Format("<?xml version=\"1.0\" encoding=\"US-ASCII\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}</name><SSIDConfig><SSID><name>{0}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><autoSwitch>false</autoSwitch><MSM><security><authEncryption><authentication>{2}</authentication><encryption>{3}</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey></security></MSM> </WLANProfile>", SelectedNetwork.ProfileName, tbWiFiKey.Text, DefaultAuthAlgorithm, DefaultCipherAlgorithm);

            Wlan.WlanReasonCode code;
            Wlan.WlanSetProfile(phClientHandle, SelectedInterface.InterfaceGuid, Wlan.WlanProfileFlags.AllUser, profileXml, null, false, pReserved, out code);
            Wlan.WlanConnect(phClientHandle, SelectedInterface.InterfaceGuid,ref ConParams, pReserved);

            timerConnection.Enabled = true;
        }
Example #4
0
            /// <summary>
            /// Connects to a network using profile and preferred list of APs
            /// </summary>
            /// <param name="connectionMode"></param>
            /// <param name="bssType">Should match the type in the profile</param>
            /// <param name="bssMacs">The MACs of APs to attempt to connect to</param>
            /// <param name="profile">profile for the relevant SSID</param>
            /// mostly taken from https://social.msdn.microsoft.com/Forums/de-DE/d11150ae-9bb7-4e44-9c57-6a411b9772d7/cant-connect-to-a-specific-ap-using-wlanconnect?forum=vistawirelesssdk
            public void ConnectBSS(Wlan.WlanConnectionMode connectionMode, Wlan.Dot11BssType bssType, byte[][] bssMacs, string profile)
            {
                Wlan.WlanConnectionParameters connectionParams = new Wlan.WlanConnectionParameters();
                connectionParams.wlanConnectionMode = connectionMode;
                connectionParams.profile = profile;
                connectionParams.dot11BssType = bssType;
                connectionParams.flags = 0;

                Wlan.NDIS_OBJECT_HEADER ndoh;// = new Wlan.NDIS_OBJECT_HEADER();
                ndoh.Type = Wlan.NDIS_OBJECT_TYPE_DEFAULT;
                ndoh.Revision = Wlan.DOT11_BSSID_LIST_REVISION_1;
                ndoh.Size = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Wlan.DOT11_BSSID_LIST));
                
                Wlan.DOT11_BSSID_LIST desBssidList = new Wlan.DOT11_BSSID_LIST();
                desBssidList.Header = ndoh;
                desBssidList.uNumOfEntries = (uint)bssMacs.Length;
                desBssidList.uTotalNumOfEntries = (uint)bssMacs.Length;
                desBssidList.BSSIDs = new Wlan.DOT11_MAC_ADDRESS[bssMacs.Length];
                
                int i = 0;
                foreach (byte[] bssMac in bssMacs)
                {
                    Wlan.DOT11_MAC_ADDRESS bssid = new Wlan.DOT11_MAC_ADDRESS();
                    bssid.Dot11MacAddress = bssMac;
                    desBssidList.BSSIDs[i++] = bssid;
                }

                IntPtr desBssidListPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desBssidList));
                Marshal.StructureToPtr(desBssidList, desBssidListPtr, false);
                connectionParams.desiredBssidListPtr = desBssidListPtr;
                
                Connect(connectionParams);
            }