Exemple #1
0
        private static WlanConnectionParameters CreateConnectionParameters(WlanConnectionMode mode, string profile, PhysicalAddress[] bssids, Dot11BssType bssType, Dot11Ssid?ssid, WlanConnectionFlags flags)
        {
            WlanConnectionParameters cp = new WlanConnectionParameters();

            cp.BssType        = bssType;
            cp.ConnectionMode = mode;
            cp.Flags          = flags;
            cp.Profile        = profile;

            Dot11BssidList bssidList = new Dot11BssidList();

            if (bssids != null)
            {
                Dot11MacAddress[] macs = Util.ConvertPhysicalAddresses(bssids);
                bssidList           = Dot11BssidList.Build(macs);
                cp.DesiredBssidList = Marshal.AllocHGlobal(bssidList.Header.Size);
                Int64 address = cp.DesiredBssidList.ToInt64();
                Marshal.StructureToPtr(bssidList.Header, new IntPtr(address), false);
                address += Marshal.SizeOf(typeof(NdisObjectHeader));
                Marshal.StructureToPtr(bssidList.ListHeader, new IntPtr(address), false);
                address += Marshal.SizeOf(typeof(Dot11BssidListHeader));
                Int64 offset = Marshal.SizeOf(typeof(Dot11MacAddress));
                for (int i = 0; i < bssidList.Entries.Length; i++)
                {
                    Marshal.StructureToPtr(bssidList.Entries[i], new IntPtr(address), false);
                    address += offset;
                }
            }
            if (ssid.HasValue)
            {
                cp.Ssid = Marshal.AllocHGlobal(Marshal.SizeOf(ssid.Value));
                Marshal.StructureToPtr(ssid.Value, cp.Ssid, false);
            }
            return(cp);
        }
Exemple #2
0
        /// <summary>
        /// Attempts to connect using specified parameters. Method returns after successful connection has been established or on timeout.
        /// </summary>
        /// <param name="conParam">Structure containing connection parameters.</param>
        /// <param name="timeout">Timeout after which method returns unsuccessfully.</param>
        /// <returns>Value indicating whether connection attempt finished successfully within specified period of time.</returns>
        /// <remarks><paramref name="conParam"/> must be prepared beforehand using <see cref="CreateConnectionParameters"/> and released afterwards using <see cref="DestroyConnectionParameters"/>.</remarks>
        private bool ConnectSync(WlanConnectionParameters conParam, int timeout)
        {
            Object key   = new Object();
            bool   value = false;
            bool   quit  = false;

            AcmConnectionEventHandler successHandler = (sender, e) => {
                lock (key) {
                    if (!quit)
                    {
                        //TODO if profile name equals
                        value = true;
                        quit  = true;
                        Monitor.Pulse(key);
                    }
                }
            };

            AcmConnectionEventHandler failureHandler = (sender, e) => {
                lock (key) {
                    if (!quit)
                    {
                        //TODO if profile name equals
                        value = false;
                        quit  = true;
                        Monitor.Pulse(key);
                    }
                }
            };

            System.Timers.ElapsedEventHandler timerHandler = (sender, e) => {
                lock (key) {
                    quit = true;
                    Monitor.Pulse(key);
                }
            };

            System.Timers.Timer timer = new System.Timers.Timer(timeout);
            timer.AutoReset = false;
            timer.Elapsed  += timerHandler;

            try {
                lock (key) {
                    AcmConnectionCompleted     += successHandler;
                    AcmConnectionAttemptFailed += failureHandler;
                    timer.Start();
                    Connect(conParam);
                    while (!quit)
                    {
                        Monitor.Wait(key);
                    }
                }
            } finally {
                timer.Stop();
                AcmConnectionCompleted     -= successHandler;
                AcmConnectionAttemptFailed -= failureHandler;
            }
            return(value);
        }
Exemple #3
0
        /// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile)
        {
            WlanConnectionParameters connectionParams = new WlanConnectionParameters();

            connectionParams.wlanConnectionMode = connectionMode;
            connectionParams.profile            = profile;
            connectionParams.dot11BssType       = bssType;
            connectionParams.flags = 0;
            Connect(connectionParams);
        }
Exemple #4
0
        public void Connect(WlanConnectionMode mode, string profile, PhysicalAddress[] bssids, Dot11BssType bssType, Dot11Ssid?ssid, WlanConnectionFlags flags)
        {
            WlanConnectionParameters cp = new WlanConnectionParameters();

            try {
                cp = CreateConnectionParameters(mode, profile, bssids, bssType, ssid, flags);
                Connect(cp);
            } finally {
                DestroyConnectionParameters(cp);
            }
        }
Exemple #5
0
        /// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile)
        {
            var connectionParams = new WlanConnectionParameters
            {
                wlanConnectionMode = connectionMode,
                profile            = profile,
                dot11BssType       = bssType,
                flags = 0
            };

            Connect(connectionParams);
        }
Exemple #6
0
 private static void DestroyConnectionParameters(WlanConnectionParameters cp)
 {
     if (cp.DesiredBssidList != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(cp.DesiredBssidList);
         cp.DesiredBssidList = IntPtr.Zero;
     }
     if (cp.Ssid != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(cp.Ssid);
         cp.Ssid = IntPtr.Zero;
     }
 }
Exemple #7
0
        public bool ConnectSync(WlanConnectionMode mode, string profile, PhysicalAddress[] bssids, Dot11BssType bssType, Dot11Ssid?ssid, WlanConnectionFlags flags, int timeout)
        {
            WlanConnectionParameters cp = new WlanConnectionParameters();
            bool value;

            try {
                cp    = CreateConnectionParameters(mode, profile, bssids, bssType, ssid, flags);
                value = ConnectSync(cp, timeout);
            } finally {
                DestroyConnectionParameters(cp);
            }
            return(value);
        }
Exemple #8
0
        /// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, Dot11Ssid ssid, WlanConnectionFlags flags)
        {
            WlanConnectionParameters connectionParams = new 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);
        }
Exemple #9
0
 /// <summary>
 /// Connects to a network defined by a connection parameters structure.
 /// </summary>
 /// <param name="connectionParams">The connection paramters.</param>
 protected void Connect(WlanConnectionParameters connectionParams)
 {
     WlanInterop.ThrowIfError(WlanInterop.WlanConnect(client.clientHandle, info.interfaceGuid, ref connectionParams, IntPtr.Zero));
 }
Exemple #10
0
 /// <summary>
 /// Attempts to connect using specified parameters. Method returns immediately. Progress is reported through <see cref="AcmConnectionCompleted"/> event.
 /// </summary>
 /// <param name="conParam">Structure containing connection parameters.</param>
 /// <remarks><paramref name="conParam"/> must be prepared beforehand using <see cref="CreateConnectionParameters"/> and released afterwards using <see cref="DestroyConnectionParameters"/>.</remarks>
 private void Connect(WlanConnectionParameters connectionParams)
 {
     Util.ThrowIfError(NativeMethods.WlanConnect(client.clientHandle, Guid, ref connectionParams, IntPtr.Zero));
 }
Exemple #11
0
 internal static extern int WlanConnect(
     [In] IntPtr clientHandle,
     [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
     [In] ref WlanConnectionParameters connectionParameters,
     IntPtr pReserved);