WiFi Scanner using C#

Posted by Samath
  4378
January 01, 2016

 

Wi-Fi Scanner allows you to easily locate visible wireless networks and its corresponding information. The tool obtains the network name (SSID), signal strength (RSSI) and quality, MAC address (BSSID), channel, maximum and achievable data rate, security, and much more. Wi-Fi Scanner is useful for normal access point users who need to find out the signal strength distribution for their wireless network at home, or choose a position for their access point for optimal signal quality.

In Order for the program to work these two classes are require. Here is the download link for the files

https://www.dropbox.com/sh/hwly57se5x4u66z/AAAjW_MdRnjLpSQVQKE7cZaTa?dl=0

Taskbar Class:

public static class Taskbar
    {
        public enum TaskbarStates
        {
            NoProgress = 0,
            Indeterminate = 0x1,
            Normal = 0x2,
            Error = 0x4,
            Paused = 0x8
        }

        [ComImportAttribute()]
        [GuidAttribute("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        private interface ITaskbarList3
        {
            [PreserveSig]
            void HrInit();
            [PreserveSig]
            void AddTab(IntPtr hwnd);
            [PreserveSig]
            void DeleteTab(IntPtr hwnd);
            [PreserveSig]
            void ActivateTab(IntPtr hwnd);
            [PreserveSig]
            void SetActiveAlt(IntPtr hwnd);

            [PreserveSig]
            void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);

            [PreserveSig]
            void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
            [PreserveSig]
            void SetProgressState(IntPtr hwnd, TaskbarStates state);
        }

        [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
        [ClassInterfaceAttribute(ClassInterfaceType.None)]
        [ComImportAttribute()]
        private class TaskbarInstance { }

        private static ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();

        private static bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);

        public static void SetState(IntPtr windowHandle, TaskbarStates taskbarState)
        {
            if (taskbarSupported) taskbarInstance.SetProgressState(windowHandle, taskbarState);
        }

        public static void SetValue(IntPtr windowHandle, double progressValue, double progressMax)
        {
            if (taskbarSupported) taskbarInstance.SetProgressValue(windowHandle, (ulong)progressValue, (ulong)progressMax);
        }
    }

 

Main Code:

using System;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WiFi.Scanner.Classes;

namespace WiFi.Scanner
{
    
    public partial class FormScanner : Form
    {

        private WlanClient wlanClient = null;
        

        public FormScanner()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void FormScanner_Load(object sender, EventArgs e)
        {
            wlanClient = new WlanClient();

            Taskbar.SetState(this.Handle, Taskbar.TaskbarStates.Indeterminate);
        }
     
        private void FormScanner_Shown(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            try
            {
                this.ScanRegister();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, WiFiThemeContainer.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Cursor = Cursors.Default;
        }
        
        

        private void ScanRegister()
        {
            listViewAccessPoints.Items.Clear();
            
            foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
            {
                Wlan.WlanAvailableNetwork[] networks = wlanInterface.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludeAllAdhocProfiles |
                                                                                             Wlan.WlanGetAvailableNetworkFlags.IncludeAllManualHiddenProfiles);
                Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList();

                wlanInterface.WlanNotification += 
                    new WlanClient.WlanInterface.WlanNotificationEventHandler(Wlan_Notification);

                this.NetworkList(networks, wlanBssEntries);
            }
        }
        private void Scan()
        {
            listViewAccessPoints.Items.Clear();

            foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
            {
                Wlan.WlanAvailableNetwork[] networks = wlanInterface.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludeAllAdhocProfiles |
                                                                                              Wlan.WlanGetAvailableNetworkFlags.IncludeAllManualHiddenProfiles);
                Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList();

                this.NetworkList(networks, wlanBssEntries);
            }
        }

        private void ResetList(bool reset)
        {
            if (reset) listViewAccessPoints.Items.Clear();
            listViewAccessPoints.Refresh();
        }
        private string GetProfileName(Wlan.Dot11Ssid value)
        {
            return Encoding.ASCII.GetString(value.SSID, 0, (int)value.SSIDLength);
        }
        private string GetMacAddress(Byte[] value)
        {
            var macAddrLen = (uint)value.Length;
            var str = new string[(int)macAddrLen];

            for (int i = 0; i < macAddrLen; i++)
                str[i] += (value[i].ToString("x2").PadLeft(2, '0').ToUpper() +":").Trim();

            str[str.Length - 1] = str[str.Length - 1].Remove(2, 1);

            return string.Join("", str);
        }
        private int GetChannel(Wlan.WlanBssEntry value)
        {
            int freq = (int)(value.chCenterFrequency / 1000);

            if (freq >= 2412 && freq <= 2484)
                return (freq - 2412) / 5 + 1;
            else if (freq >= 5170 && freq <= 5825)
                return (freq - 5170) / 5 + 34;
            else
                return -1;
        }
        private void ShowProgress(int value, int max,Taskbar.TaskbarStates status)
        {
            if (status == Taskbar.TaskbarStates.NoProgress)
            {
                Taskbar.SetState(this.Handle, Taskbar.TaskbarStates.NoProgress);
                return;
            }

            Taskbar.SetValue(this.Handle, value, max);
            Taskbar.SetState(this.Handle, status);
        }

        private void NetworkList(Wlan.WlanAvailableNetwork[] networks, Wlan.WlanBssEntry[] wlanBssEntries)
        {
            int count = 0;

            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                Application.DoEvents();

                Wlan.WlanBssEntry entry = (from bs in wlanBssEntries
                                           where GetProfileName(bs.dot11Ssid).Trim() == GetProfileName(network.dot11Ssid).Trim()
                                           select bs).FirstOrDefault<Wlan.WlanBssEntry>();

                ShowProgress((count += 1), networks.Length, Taskbar.TaskbarStates.Normal);
                this.AddToList(network, entry);

                Thread.Sleep(500);
            }

            ShowProgress(0, 0, Taskbar.TaskbarStates.NoProgress);
        }
        private void AddToList(Wlan.WlanAvailableNetwork network, Wlan.WlanBssEntry entry)
        {
            ListViewItem wifiItem = new ListViewItem(this.GetProfileName(network.dot11Ssid));

            // MAC Address
            wifiItem.SubItems.Add(this.GetMacAddress(entry.dot11Bssid));
            // Signal Quality
            wifiItem.SubItems.Add(string.Format("{0}%", network.wlanSignalQuality.ToString()));
            // dBm Value
            wifiItem.SubItems.Add(string.Format("{0}dBm", entry.rssi.ToString()));
            // Channel No
            wifiItem.SubItems.Add(this.GetChannel(entry).ToString());
            // Encryption
            wifiItem.SubItems.Add(network.dot11DefaultCipherAlgorithm.ToString());
            // Authentication
            wifiItem.SubItems.Add(network.dot11DefaultAuthAlgorithm.ToString());
            
            int range = ((int)network.wlanSignalQuality - 1) / 25;
            wifiItem.ImageIndex = range;

            if (network.dot11DefaultCipherAlgorithm.ToString().Equals("None"))
                wifiItem.BackColor = Color.LimeGreen;

            listViewAccessPoints.Items.Add(wifiItem);
        }
        

        private void Wlan_Notification(Wlan.WlanNotificationData notifyData)
        {
            switch (notifyData.notificationSource)
            {
                case Wlan.WlanNotificationSource.ACM:
                    switch ((Wlan.WlanNotificationCodeAcm)notifyData.NotificationCode)
                    {
                        case Wlan.WlanNotificationCodeAcm.ScanComplete:
                            
                            this.Cursor = Cursors.WaitCursor;

                            try
                            {
                                this.Scan();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, WiFiThemeContainer.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            this.Cursor = Cursors.Default;

                            break;
                    }
                    break;
            }
        }
        
    }
}

 

   
  
 
 
   

 
Abelnegash2015
November 22, 2016
 
please send me the full running file