/// <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 WlanBssEntry[] GetNetworkBssList(Dot11Ssid ssid, Dot11BssType bssType, bool securityEnabled) { IntPtr ssidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid)); Marshal.StructureToPtr(ssid, ssidPtr, false); try { IntPtr bssListPtr; WlanInterop.ThrowIfError(WlanInterop.WlanGetNetworkBssList(client.clientHandle, info.interfaceGuid, ssidPtr, bssType, securityEnabled, IntPtr.Zero, out bssListPtr)); try { return(ConvertBssListPtr(bssListPtr)); } finally { WlanInterop.WlanFreeMemory(bssListPtr); } } finally { Marshal.FreeHGlobal(ssidPtr); } }
public static extern int WlanGetNetworkBssList( [In] IntPtr clientHandle, [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, [In] IntPtr dot11SsidInt, [In] Dot11BssType dot11BssType, [In] bool securityEnabled, IntPtr reservedPtr, [Out] out IntPtr wlanBssList );
internal static extern int WlanGetNetworkBssList( [In] IntPtr hClientHandle, [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, [In, Optional] IntPtr pDot11Ssid, [In] Dot11BssType dot11BssType, [In] bool bSecurityEnabled, [In, Out] IntPtr pReserved, [Out] out IntPtr ppWlanBssList );
/// <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); }
/// <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); }
/// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. Stopwatch sw = new Stopwatch(); try { Connect(connectionMode, bssType, profile); sw.Start(); while (queueEvents && eventQueueFilled.WaitOne(connectTimeout, true)) { lock (eventQueue) { while (eventQueue.Count != 0) { if (sw.ElapsedMilliseconds > connectTimeout) { throw new Exception("Timeout"); } 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 == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.notifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.connNotifyData.profileName == profile) { return(true); } break; } } break; } } } } } finally { sw.Stop(); queueEvents = false; eventQueue.Clear(); } return(false); // timeout expired and no "connection complete" }
/// <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); }
/// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { _queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. 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)) { continue; } WlanConnectionNotificationEventData wlanConnectionData = (WlanConnectionNotificationEventData)e; // Check if the conditions are good to indicate either success or failure. if (wlanConnectionData.NotifyData.notificationSource == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.NotifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.ConnNotifyData.profileName == profile) { return(true); } break; } } break; } } } } finally { _queueEvents = false; lock (_eventQueue) { _eventQueue.Clear(); } } return(false); // timeout expired and no "connection complete" }
/// <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(WlanConnectionMode connectionMode, Dot11BssType bssType, string profile, int connectTimeout) { queueEvents = true; // NOTE: This can cause side effects, other places in the application might not get events properly. 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 == WlanNotificationSource.MSM) { switch ((WlanNotificationCodeMsm)wlanConnectionData.notifyData.notificationCode) { case WlanNotificationCodeMsm.Connected: if (wlanConnectionData.connNotifyData.profileName == profile) return true; break; } } break; } } } } } finally { queueEvents = false; eventQueue.Clear(); } return false; // timeout expired and no "connection complete" }
/// <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 WlanBssEntry[] GetNetworkBssList(Dot11Ssid ssid, Dot11BssType bssType, bool securityEnabled) { IntPtr ssidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid)); Marshal.StructureToPtr(ssid, ssidPtr, false); try { IntPtr bssListPtr; WlanInterop.ThrowIfError(WlanInterop.WlanGetNetworkBssList(client.clientHandle, info.interfaceGuid, ssidPtr, bssType, securityEnabled, IntPtr.Zero, out bssListPtr)); try { return ConvertBssListPtr(bssListPtr); } finally { WlanInterop.WlanFreeMemory(bssListPtr); } } finally { Marshal.FreeHGlobal(ssidPtr); } }
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); }
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); }
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); } }