/// <summary> /// This function fetches the ssid of wlan connection for the system /// </summary> /// <returns>A wlaninfo object which contains SSID and security info</returns> private WlanInfo GetWirelessConnection() { WlanInfo wifiInfo = new WlanInfo(); IntPtr handle = IntPtr.Zero; uint negotiatedVersion; try { if (NativeMethods.WlanOpenHandle(1, IntPtr.Zero, out negotiatedVersion, out handle) != 0) { return(null); } IntPtr ptr = new IntPtr(); if (NativeMethods.WlanEnumInterfaces(handle, IntPtr.Zero, ref ptr) != 0) { return(null); } Structs.WlanInterfaceInfoList infoList = new Structs.WlanInterfaceInfoList(ptr); NativeMethods.WlanFreeMemory(ptr); Guid guid; uint dataSize; Structs.WlanConnectionAttributes connection; // Call wlanqueryinterface for all the interfaces in the list for (int i = 0; i < infoList.dwNumberOfItems; i++) { guid = infoList.InterfaceInfo[i].InterfaceGuid; if (NativeMethods.WlanQueryInterface(handle, ref guid, Wlan.Core.Enums.WlanIntfOpcode.wlan_intf_opcode_current_connection, IntPtr.Zero, out dataSize, ref ptr, IntPtr.Zero) != 0) { return(null); } connection = (Structs.WlanConnectionAttributes)Marshal.PtrToStructure(ptr, typeof(Structs.WlanConnectionAttributes)); byte[] arr = connection.wlanAssociationAttributes.dot11Ssid.ucSSID; string ssid = Encoding.UTF8.GetString(arr); wifiInfo.SSID = ssid.Trim('\0'); wifiInfo.IsSecured = connection.wlanSecurityAttributes.bSecurityEnabled; NativeMethods.WlanFreeMemory(ptr); } return(wifiInfo); } #pragma warning disable CA1031 // pinvokes might throw runtime exception catch (Exception) #pragma warning restore CA1031 { return(null); } finally { if (handle != IntPtr.Zero) { uint returncode = NativeMethods.WlanCloseHandle(handle, IntPtr.Zero); } } }
public WlanInfo GetWlanInfo(WlanType type) { var urn = $"urn:dslforum-org:service:WLANConfiguration:{(int)type}"; var action = "GetInfo"; var xml = GetRequestXml(action, urn); var response = RequestWithAuthentication(action, urn, xml); var node = response.GetElementsByTagName("u:GetInfoResponse")[0]; var se = new WlanInfo(node); return(se); }