Haim Shabort
Haim Shabort

Reputation: 57

How can i scan and list all connected devices to my network wireless, i'm getting exception?

My pc is connected to the router of the network i want to scan but the not wireless the pc is connected with a cable to the router. But my android device is connected to the network wireless.

So in logic in this case the results in the list should be my pc and my android device.

This is what i'm using now managed wifi api:

managed wifi api

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NativeWifi;

namespace ScanWifi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            WlanClient client = new WlanClient();
            try
            {
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {

                    Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();

                    foreach (Wlan.WlanBssEntry network in wlanBssEntries)
                    {
                        int rss = network.rssi;
                        byte[] macAddr = network.dot11Bssid;

                        string tMac = "";

                        for (int i = 0; i < macAddr.Length; i++)
                        {

                            tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();

                        }

                        listView1.Items.Add("Found network with SSID {0}." + System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString());
                        listView1.Items.Add("Signal: {0}%."+ network.linkQuality);
                        listView1.Items.Add("BSS Type: {0}."+ network.dot11BssType);
                        listView1.Items.Add("MAC: {0}.", tMac);
                        listView1.Items.Add("RSSID:{0}", rss.ToString());

                    }
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

When running the program i'm exception on WlanApi.cs on the line:

Wlan.ThrowIfError(
                Wlan.WlanOpenHandle(Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle));

System.ComponentModel.Win32Exception' occurred in ManagedWifi.dll The service has not been started

Upvotes: 0

Views: 2183

Answers (2)

C Robinson
C Robinson

Reputation: 469

For Windows 10, the service "WLAN AutoConfig" must be started for WlanClient to work. This service should be started automatically on a computer which has a WiFi adapter present. On a computer such as a desktop which does not have a WiFi adapter, the service startup type is probably Manual and not started; you can start it anyway and WlanClient should no longer throw any exceptions, but without a WiFi adapter, it won't see any interfaces, so you won't be able to get a list of networks.

Upvotes: 1

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

According to the documentation of the [WlanOpenHandle ][1] function, the problem is that the Wireless Zero Configuration (WZC) service is not started on your machine:

WlanOpenHandle will return an error message if the Wireless Zero Configuration (WZC) service has not been started or if the WZC service is not responsive.

However, depending on your platform, it might also might be the case that you are simply passing the wrong parameters to the WlanOpenHandle function. Have you tried passing Wlan.WLAN_CLIENT_VERSION_LONGHORN as the first parameter?

Upvotes: 0

Related Questions