WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Native WIFI API

 Keywords:  Native WIFI Connections API LAN List Enumerate Interfaces WlanEnumInterfaces WlanGetNetworkBssList WlanFreeMemory WlanOpenHandle WlanCloseHandle wlanapi.dll DllCall

UPDATE:

Use the WLAN Extender: http://files.winbatch.com/wwwftp/wb01/wwlan44i.zip

Question:

Does anyone know of a way to get the name of the active access point / wifi network? I'd like to have a script that triggers on wake of my laptop and adjusts my DNS settings based on which network is discovered and connected to by Windows.

Answer:

I don't believe the WIFI WMI classes are supported on Windows 7 and newer version of Windows. I haven't tried it so I can't guarantee success but there is a .Net class the wraps the Windows Native WIFI API and if properly written, should work with WinBatch's dotNet system. It can be found here http://managedwifi.codeplex.com/. You could also take a look at using the WinBatch 'DllCall' function to directly access the Native WIFI API. The API documentation can be found here http://msdn.microsoft.com/en-us/library/windows/desktop/ms706556(v=vs.85).aspx.

The idea of creating a Native WIFI WIL extender has been kicked around a bit. We have been more or less expecting MSFT to offer something in the FCL (dotNet Framework) so we haven't taken the plunge. We did do some prototyping with WinBatch to familiarize ourselves with the API. Apologies in advance for this script as it contains a lot of magic numbers and minimal documentation or error checking, but it does seem to kinda work on a Windows 8 notebook computer.

#DefineFunction MemToStr(_pMem, _nLen)
   hTemp = BinaryAlloc(_nLen+1)
   For x = 0 To _nLen - 1
      BinaryPoke( hTemp, x, IntControl(32,_pMem+x, "BYTE", 0,0) )
   Next
   strRet = BinaryPeekStr(hTemp, 0, _nLen)
   BinaryFree(hTemp)
   Return strRet
#EndFunction

lSsids = "<No Connections>" ; or is that El Cid?

While 1
   nRetCode = 0
   hWlanApi = DllLoad(DirWindows(1):"wlanapi.dll")

   hNetVer = BinaryAlloc(4)
   BinaryEodSet(hNetVer, 4)
   hClient = BinaryAlloc(4)
   BinaryEodSet(hClient, 4)

   ; Need a client handle for amost every function.
   nRetCode = DllCall(hWlanApi, long:"WlanOpenHandle", long:2, long:0, lpBinary:hNetVer, lpBinary:hClient)
   If nRetcode Then Break ; Error

   hClientHandle = BinaryPeek4(hClient, 0)
   hpLfList = BinaryAlloc(4)
   BinaryEodSet(hpLfList, 4)
   nRetCode = DllCall(hWlanApi, long:"WlanEnumInterfaces",long:hClientHandle, long:0, lpBinary:hpLfList)
   If nRetCode Then Break ; Error

   ; Number of interfaces is first structure member.
   pLfList = BinaryPeek4(hpLfList, 0)
   dwNumberOfItems = IntControl(32, pLfList, "LONG", 0, 0)

   ; Find a connected WIFI interface
   pInfoArray     = pLfList + 8  ; Pointer to array of infos is at this offset in the structure
   nInfoSize      = 16 + (256*2) + 4 ; Size of each structure in array.
   nisStateOffset = 16 + (256*2)       ; Offset of state member.
   isState        = 0
   For nIndex = 0 To dwNumberOfItems
      isState = IntControl(32,pInfoArray+nisStateOffset, "LONG", 0, 0)
      If isState == 1 Then Break ; Found one! - 1 means connected
      pInfoArray = pInfoArray + nInfoSize
   Next

   ; Get the ssid as stored in the Bss list.
   If isState == 1
      lSsids = ""
      hpBssList = BinaryAlloc(4)
      If !hpBssList Then Break
      BinaryEodSet(hpBssList, 4)

      nRetCode = DllCall(hWlanApi, long:"WlanGetNetworkBssList",long:hClientHandle, long:pInfoArray, long:0, long:0, long:0, long:0, lpbinary:hpBssList)
      If nRetCode Then Break ; Error

      ;  Get a SSID from a BSS structure
      pBssList  = BinaryPeek4(hpBssList, 0)
      nEntries  = IntControl(32,pBssList+4, "LONG", 0, 0)  ; Number of elements in bss array here.
      pBssEntry = pBssList+8 ; Offset of bss array in list structure.
      For nIndex = 0 To  nEntries -1

         ; The length in chars of the SSID.
         nSsidLen = IntControl(32,pBssEntry, "LONG", 0, 0)

         ; Offset location of the SSID member.
         pSsid = pBssEntry + 4

         ; Get the SSID as a string.
         strSsid = MemToStr(pSsid, nSsidLen)

         ; lRssi stucture member contains the signal strength in dBm
         nSigStren = IntControl(32,pBssEntry+60, "LONG", 0,0) ; Strength at offset 60

         ; Add to our output.
         lSsids = ItemInsert( "SSID: ":strSsid:@TAB:"Signal Strength: ":nSigStren:" (dBm)", -1, lSsids, @CR)

         ; Move to next entry in array.
         pBssEntry = pBssEntry + 140 ; 2 extra bytes added because of dot11Bssid member struct alignment.
      Next

   EndIf
   Break ; out of while
EndWhile

; Clean up as best we can.
If IsDefined(hTemp) Then If hTemp Then BinaryFree( hTemp)
If IsDefined(hpBssList) Then If hpBssList Then Then BinaryFree(hpBssList)
If IsDefined(hLfList) Then If hLfList Then  BinaryFree(hLfList)
If IsDefined(hClient) Then If hClient Then  BinaryFree(hClient)
If IsDefined(hNetVer) Then If hNetVer Then BinaryFree(hNetVer)

If IsDefined(pLfList) Then If pLfList Then DllCall(hWlanApi, void:"WlanFreeMemory",long:pLfList)
If IsDefined(pBssList) Then If pBssList Then DllCall(hWlanApi, void:"WlanFreeMemory",long:pBssList)

If hClientHandle
   nRetCode = DllCall(hWlanApi, long:"WlanCloseHandle", long:hClientHandle, long:0)
EndIf
DllFree(hWlanApi)

;  Emtpy test would mean no WIFI connections.
If !nRetCode Then Message("WIFI Connection Info",lSsids)
Else Message("Native WIFI API Error", nRetCode)
Exit

Article ID:   W17794
Filename:   Native WIFI API.txt
File Created: 2014:06:27:12:15:24
Last Updated: 2014:06:27:12:15:24