Example #1
0
        public static void ApplyWlanInformation(this List<NetworkAdapter> Adapters)
        {
            try
            {
                IntPtr wlanHandle;
                uint wlanServiceVersion;

                if (WlanApi.WlanOpenHandle(WlanApi.WLAN_API_VERSION_2_0, IntPtr.Zero, out wlanServiceVersion, out wlanHandle) == WlanApi.ERROR_SUCCESS)
                {
                    try
                    {
                        IntPtr wlanInterfacesPtr;

                        if (WlanApi.WlanEnumInterfaces(wlanHandle, IntPtr.Zero, out wlanInterfacesPtr) == WlanApi.ERROR_SUCCESS)
                        {
                            try
                            {
                                var wlanInterfaces = new WLAN_INTERFACE_INFO_LIST(wlanInterfacesPtr);

                                foreach (var wlanInterface in wlanInterfaces.InterfaceInfo)
                                {
                                    var adapter = Adapters.FirstOrDefault(a => a.ConnectionIdentifier == wlanInterface.InterfaceGuid);
                                    if (adapter != null)
                                    {
                                        adapter.IsWlanAdapter = true;
                                        adapter.WlanStatus = wlanInterface.isState.Description();
                                    }
                                }
                            }
                            finally
                            {
                                WlanApi.WlanFreeMemory(wlanInterfacesPtr);
                            }
                        }
                    }
                    finally
                    {
                        WlanApi.WlanCloseHandle(wlanHandle, IntPtr.Zero);
                    }
                }
            }
            catch (DllNotFoundException)
            {
                // Ignore
                // Indicates 'Wlanapi.dll' isn't present (ie. Servers)
            }
            catch (Exception ex)
            {
                throw new Exception("Disco Client was unable to retrieve Wireless NetworkAdapter information from WlanApi", ex);
            }
        }
Example #2
0
        private static List<WirelessProfile> GetWirelessProfiles(IntPtr wlanHandle)
        {
            uint interopResult;
            IntPtr wlanInterfacesPtr;

            // Enumerate wireless interfaces
            interopResult = WlanApi.WlanEnumInterfaces(wlanHandle, IntPtr.Zero, out wlanInterfacesPtr);
            if (interopResult != WlanApi.ERROR_SUCCESS)
            {
                throw new Exception($"Unable to list interfaces with the local wireless service. WlanEnumInterfaces returned: {interopResult}");
            }
            try
            {
                var wlanInterfaces = new WLAN_INTERFACE_INFO_LIST(wlanInterfacesPtr);
                var profiles = new List<WirelessProfile>();

                foreach (var wlanInterface in wlanInterfaces.InterfaceInfo)
                {
                    IntPtr wlanProfilesPtr;
                    // Enumerate wireless profiles for interface
                    interopResult = WlanApi.WlanGetProfileList(wlanHandle, wlanInterface.InterfaceGuid, IntPtr.Zero, out wlanProfilesPtr);
                    if (interopResult != WlanApi.ERROR_SUCCESS)
                    {
                        throw new Exception($"Unable to list wireless profiles for the {wlanInterface.InterfaceGuid} interface with the local wireless service. WlanGetProfileList returned: {interopResult}");
                    }
                    try
                    {
                        var wlanProfiles = new WLAN_PROFILE_INFO_LIST(wlanProfilesPtr);

                        foreach (var wlanProfile in wlanProfiles.ProfileInfo)
                        {
                            profiles.Add(new WirelessProfile()
                            {
                                Name = wlanProfile.strInterfaceDescription,
                                InterfaceGuid = wlanInterface.InterfaceGuid,
                                IsGroupPolicy = wlanProfile.dwFlags.HasFlag(ProfileInfoFlags.WLAN_PROFILE_GROUP_POLICY)
                            });
                        }
                    }
                    finally
                    {
                        WlanApi.WlanFreeMemory(wlanProfilesPtr);
                    }
                }

                return profiles;
            }
            finally
            {
                WlanApi.WlanFreeMemory(wlanInterfacesPtr);
            }
        }