Click here to Skip to main content
Click here to Skip to main content

WLAN Scan with NDIS Miniport and Much More

, 29 Oct 2007
Rate this:
Please Sign up or sign in to vote.
An article about using NDIS Miniport from userspace to access several functions of the WLANcard
Screenshot - screen.jpg


I spent some time with WLAN, and I wondered how NetStumbler performed this nice scan, but the source is not free. I programmed a lot of stuff but nothing worked. I tried to code my own driver and used WinPcap, but nothing worked very well. Then I read an article, "Scanning for Wireless Networks" in 29A magazine 8 from GriYo. My work is mainly based on this article and you can find a lot of sources on the internet that do the same thing.


It's possible via NDIS Miniport (from userspace) to access a lot of functions of the WLANcard. To get access to all these nice functions you need the Windows DDK.

Take a look at ntddndis.h and you will find a lot of actions you can perform. For example, you can get or set the WEP key. I only implemeted the scanning function, but you can improve it. To build your own application, you can take my article class or write our own.

Using the Code

Well, I put a lot of comments in the code so you will be able to understand it easily. All the functions you need are in the airctl class. Take a look at the header file.

class airctl
    HANDLE m_handle;
    deviceInfo* m_devices;
        // Frees the list of wlans
    void freeScanList(void);
    //scans for wlan and returns pointer to the list.
    NDIS_802_11_BSSID_LIST* scan(void);

bool open( char *device_name);
    //lists all devices and saves them in an internal list.
    BOOL list_devices( void);
    // return the pointer to the list
    inline deviceInfo* getDevList( ){return m_devices;};
    // adds one device to the list
    void AddDevice(char * desc, char * name);
    // clears the intern list
    void clearDeviceList(void);
    NDIS_802_11_BSSID_LIST* m_pBSSIDList;

    // intern used to get more informations about an interface
    BOOL get_device_info(   int Index,
                        char *key_name,
                        char *device_info,
                        char *device_description);


First we need a list of all devices. Call list_devices() and it will list everything in an single linked list. Then the demo program shows the user a list of all available devices. The structure for the list is as follows.

struct deviceInfo
    char *description;//information for the user
    char *name;// important to open the device
    deviceInfo* next;//single linked list
Look at the following piece of code. It is from the demo program.
   //return me the pointer to the list
    deviceInfo* dv = win->m_airctl.getDevList();

    if (dv == NULL)
        List.AddString("No available interface found !");
        while (dv != NULL)//step through the linked list
            List.AddString(dv->description);// adds it to the list.
            dv = dv->next;

After the user selects a device, it will be opened.

To open the device with the aitctl class we only need to call open() with the device name in it.

    if(>name)!= true)
            MessageBox("Cant open selected device","fehler",0);
            ::AfxBeginThread( threadFunc , (LPVOID) this);


But we are curious and will take a small look into that function.

    //we open the physical device with CreateFileA
        m_handle = CreateFileA(   device_file,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,// share 
                                                                   // mode 
                                NULL, // default security attributes 
                                OPEN_EXISTING,// disposition 
                                0,// file attributes
                                NULL) ;// do not copy file attributes

        if( m_handle == INVALID_HANDLE_VALUE)
               return false;
               // ... open
            return true;


Now it's time to search the WLANs. We will call scan() from the aitctl class.

        NDIS_802_11_BSSID_LIST * pBSSIDList = pDlg->m_airctl.scan();

How is this performed? It's done via DeviceIoControl. This is for direct input and output, or for retrieving information from a physical device. For more infomration read this article. It's a really nice series of articles.

First we force the WLAN device to scan for WLANs; we wait for a moment and then we ask the device again to tell us an answer. The source code for this looks like this:

     oidcode = OID_802_11_BSSID_LIST_SCAN ;//action to perform

        DeviceIoControl(        m_handle,
                                sizeof( oidcode),
                                ( ULONG *) NULL,
                                NULL) ;

        Sleep( 2000) ;// we give him some time

        memset( m_pBSSIDList, 0, sizeof( NDIS_802_11_BSSID_LIST) * 
            NUMBEROF_BSSIDS) ;
        oidcode = OID_802_11_BSSID_LIST ;//action to perform

        if( DeviceIoControl(    m_handle,// device to be queried
                                IOCTL_NDIS_QUERY_GLOBAL_STATS,// operation to 
                                                              // perform
                                sizeof( oidcode),//  input buffer
                                ( ULONG *) m_pBSSIDList,// output buffer
                                sizeof( NDIS_802_11_BSSID_LIST) * 
                                &bytesreturned,// # bytes returned
                                NULL) == 0)// synchronous I/O
               // List failed
              return NULL;
        else {
              return m_pBSSIDList;

Now we have a pointer to the NDIS_802_11_BSSID_LIST take a look in ntddndis.h where it's defined.

typedef struct _NDIS_802_11_BSSID_LIST
    ULONG           NumberOfItems;      // in list below, at least 1
    NDIS_WLAN_BSSID Bssid[1];

So far so good, but what does NDIS_WLAN_BSSID look like?

typedef struct _NDIS_WLAN_BSSID
    ULONG                               Length;             // Length of this
                                                            // structure
    NDIS_802_11_MAC_ADDRESS             MacAddress;         // BSSID
    UCHAR                               Reserved[2];
    NDIS_802_11_SSID                    Ssid;               // SSID
    ULONG                               Privacy;            // WEP encryption
                                                            // requirement
    NDIS_802_11_RSSI                    Rssi;               // receive signal
                                                            // strength in dBm
    NDIS_802_11_NETWORK_TYPE            NetworkTypeInUse;
    NDIS_802_11_CONFIGURATION           Configuration;
    NDIS_802_11_NETWORK_INFRASTRUCTURE  InfrastructureMode;
    NDIS_802_11_RATES                   SupportedRates;

This looks much better. This is the information we would like to obtain. Now we can show them to the user.

But, we have one thing left to discuss: how to get the next item. For this we will use another pointer which points to NDIS_WLAN_BSSID. Now the variable ULONG Length tells us how long this entry is. After this a certain number of bytes the next entry "lies."

Well, the following is not a good solution, but it shows you what I mean and what it could look like.

    //NumberOfItems indecates how many are now in the list
    for(unsigned int i =0 ;i < pBSSIDList->NumberOfItems;i++){
        int temp=i;//used to build the differens
        //step to the next in list...
        PNDIS_WLAN_BSSID cpSsid=pBSSIDList->Bssid;//save
        //if we aren't in the first loop we have to set the pointer 
        //to the next
        while(temp!=0 ){
            // go forward
            cpSsid=(PNDIS_WLAN_BSSID)((char*)cpSsid+ cpSsid->Length);

        //do something with your data

That's it man. Thank you for reading, now it's up to you.

Points of Interest

You can perform a lot of more functions to the WLAN device. Just take a look into ntddndis.h under 802.11 OIDs.


First release version 0.1


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author


Germany Germany
No Biography provided

Comments and Discussions

QuestionHow to perform active scan Pinmembersunlikui13-Dec-11 3:41 
Questioncan not work Pinmemberzhkingsmac12-Oct-11 4:54 
QuestionDeviceIoControl return 0 in XP sp3 Pinmemberandawu11-Aug-11 0:32 
GeneralAbout the AP Scan Pinmemberfibers17-Dec-09 22:23 
QuestionProblems that are in this code: How to flush old scanned data and perform Active sacn ? Pinmemberamitalonm4-Oct-09 7:43 
AnswerRe: Problems that are in this code: How to flush old scanned data and perform Active sacn ? Pinmemberfibers17-Dec-09 22:35 
General[My vote of 1] Doesn't work at all Pinmemberwv-pankaj13-Jun-09 0:45 
Scan result is NULL always on my Win XP 32 bit where WZC finds many entires in network list. Also not working on Windows 7 32 bit.
Funny thing is default adapter on XP was not detected, however the same on Win 7 is detected.
Generalthe wlanscan_demo cannot work Pinmemberapm70pro20-Dec-08 19:52 
Generalerror link 2028 and 2019 Pinmemberwaty0019-Oct-09 7:46 
GeneralNo Scan Results Pinmembermasterluu21-Oct-08 23:26 
GeneralDeviceIoControl PinmemberJacano5-Sep-08 2:29 
QuestionHow To Connect to a Access Point? Pinmemberatrip200812-Jul-08 21:49 
AnswerRe: How To Connect to a Access Point? Pinmembersud_net23-Jul-08 23:28 
GeneralRe: How To Connect to a Access Point? Pinmemberggfernandez3-Nov-08 9:42 
Generalcan't CreateFile in xp Pinmemberpyd_jeike7-Jul-08 18:50 
GeneralRe: can't CreateFile in xp Pinmemberfibers17-Dec-09 22:38 
GeneralBugs in code PinmemberCory Atkin2-Jul-08 10:16 
GeneralRe: Bugs in code Pinmembercapitel20-Jul-08 18:58 
GeneralRe: Bugs in code - question and more problems to report Pinmemberamitalonm4-Oct-09 7:24 
GeneralRe: Bugs in code - question and more problems to report Pinmembersunlikui13-Dec-11 3:22 
GeneralRe: Bugs in code - question and more problems to report Pinmemberamitalonm13-Dec-11 10:52 
GeneralRe: Bugs in code - question and more problems to report Pinmembersunlikui13-Dec-11 14:45 
GeneralRe: Bugs in code - question and more problems to report Pinmemberamitalonm13-Dec-11 22:39 
GeneralRe: Bugs in code - question and more problems to report Pinmembersunlikui14-Dec-11 14:43 
GeneralSignal Strength Pinmembercarlosteleco28-Apr-08 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 29 Oct 2007
Article Copyright 2007 by sirb
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid