thanks to Otto for share original code, was adapted to FWPPC
Download from here: http://www.sitasoft.net/fivewin/samples/fwppc/wifi.zip
sample
- Code: Select all Expand view RUN
- #include "FWCE.ch"
FUNCTION Main()
LOCAL oWnd
LOCAL oWifi
LOCAL aAdapters, cAdapter
LOCAL n
LOCAL aInfo
LOCAL ctext
LOCAL oBrw
DEFINE DIALOG oWnd TITLE "Wifi Search" SIZE 220, 210
oWifi = TWifiScan():New()
aInfo = GetInfo( oWifi )
/*
MacAddress ( string )
Ssid ( string )
Channel ( numeric )
Auth ( string )
InfrastructureMode ( string )
Signal( numeric ) */
@ 1, 1 LISTBOX oBrw ;
FIELDS aInfo[ oBrw:nAt, 2 ], Transform(aInfo[ oBrw:nAt, 6 ], "9999" ),;
Transform( aInfo[ oBrw:nAt, 3 ], "999" ), aInfo[ oBrw:nAt, 5 ], ;
aInfo[ oBrw:nAt, 4 ], aInfo[ oBrw:nAt, 1 ] ;
HEADERS "Name", "Sig", "Ch", "Type", "Sec", "Mac";
COLSIZES 100, 30, 30, 60, 60, 150 ;
SIZE 100, 82
oBrw:SetArray( aInfo )
ACTIVATE DIALOG oWnd CENTERED;
ON CLICK( aInfo := GetInfo( oWifi, cAdapter ),;
oBrw:SetArray( aInfo ),;
oBrw:Refresh() )
oWifi:End()
RETURN NIL
FUNCTION GetInfo( oWifi )
local aInfo
local cAdapter
cAdapter = GetAdapter( oWifi )
if ! Empty( cAdapter )
oWifi:RefreshBSSIDs( cAdapter )
aInfo = oWifi:GetBBSIDs( cAdapter )
endif
if Len( aInfo ) == 0
aInfo := { { , , 0, , , 0 } }
endif
return aInfo
FUNCTION GetAdapter( oWifi )
local aAdapters := oWifi:GetAdapters()
local cAdapter := ""
if Len( aAdapters ) > 0
cAdapter = aAdapters[ 1 ]
endif
return cAdapter
CLASS
- Code: Select all Expand view RUN
#include "fivewin.ch"
CLASS TWifiScan
DATA hHandle
METHOD New()
METHOD GetAdapters INLINE GetAdapters( ::hHandle )
METHOD GetBBSIDs( cAdapter ) INLINE GetBBSIDs( ::hHandle, cAdapter )
/*Return Array 6 position
MacAddress ( string )
Ssid ( string )
Channel ( numeric )
Auth ( string )
InfrastructureMode ( string )
Signal( numeric ) */
METHOD RefreshBSSIDs( cAdapter ) INLINE RefreshBSSIDs( ::hHandle, cAdapter )
METHOD End() INLINE EndWifi( ::hHandle )
ENDCLASS
METHOD New() CLASS TWifiScan
::hHandle = InitWifi()
RETURN Self
C code
- Code: Select all Expand view RUN
#include <windows.h>
#include <hbapi.h>
#include <hbapiitm.h>
#include <ntddndis.h>
#include <nuiouser.h>
#include <winioctl.h>
typedef struct tagWifiPeek{
CRITICAL_SECTION m_Lock;
HANDLE m_hNDUIO;
} WIFIPEEK;
typedef WIFIPEEK *PFWIFIPEEK;
void CloseWifi( PFWIFIPEEK );
BOOL OpenWifi( PFWIFIPEEK );
LPWSTR AnsiToWide( LPSTR );
LPSTR WideToAnsi( LPWSTR );
const char cSep = '-';
static char * sAuth_Mode[] = { "Open System",
"Wep" };
static char * sInfra_Mode[] = { "Ad Hoc",
"Access Point",
"Auto or unknown" };
//----------------------------------
HB_FUNC( ENDWIFI )
{
PFWIFIPEEK pWifi = ( PFWIFIPEEK ) hb_parnl( 1 );
if( pWifi->m_hNDUIO != NULL)
{
CloseWifi( pWifi );
}
DeleteCriticalSection(&pWifi->m_Lock);
hb_xfree( pWifi );
}
//----------------------------------
HB_FUNC( INITWIFI )
{
PFWIFIPEEK pWifi = hb_xgrab( sizeof( WIFIPEEK ) );
pWifi->m_hNDUIO=NULL;
InitializeCriticalSection(&pWifi->m_Lock);
if( OpenWifi( pWifi ) )
hb_retnl( ( LONG ) pWifi );
else
{
hb_xfree( pWifi );
hb_retnl( 0 );
}
}
//----------------------------------
void CloseWifi( PFWIFIPEEK pWifi )
{
CloseHandle( pWifi->m_hNDUIO );
pWifi->m_hNDUIO=NULL;
}
//----------------------------------
BOOL OpenWifi( PFWIFIPEEK pWifi )
{
HANDLE hDev;
//already opened?
if( pWifi->m_hNDUIO != NULL)
{
return TRUE;
}
hDev=CreateFile(NDISUIO_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, INVALID_HANDLE_VALUE);
if(hDev == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
pWifi->m_hNDUIO=hDev;
return TRUE;
}
}
HB_FUNC( GETADAPTERS )
{
HANDLE hFile;
BYTE Buffer[2048];
VOID *pvBuf;
DWORD dwRet;
PFWIFIPEEK pWifi = ( PFWIFIPEEK ) hb_parnl( 1 );
PHB_ITEM pAdapters = hb_itemArrayNew( 0 );
if(pWifi == NULL || pWifi->m_hNDUIO == NULL)
{
hb_retnl( 0 );
}
EnterCriticalSection(&pWifi->m_Lock);
//open NDIS driver
hFile=CreateFile(L"NDS0:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
if(hFile != INVALID_HANDLE_VALUE)
{
pvBuf=(void *)(&Buffer[0]);
dwRet=sizeof(Buffer);
if(DeviceIoControl(hFile, IOCTL_NDIS_GET_ADAPTER_NAMES, NULL, 0, pvBuf, sizeof(Buffer), &dwRet, NULL))
{
//adapter list ok.
LPWSTR pszStr;
dwRet=0;
//no string classes used, so no MFC or ATL dependency.
for(pszStr=(LPWSTR)pvBuf; *pszStr; pszStr+=wcslen(pszStr)+1)
{
//check if adapter name is ok, skip infrared, gprs, ActiveSync etc.
if(wcsicmp(pszStr, L"ASYNCMAC1") && \
wcsicmp(pszStr, L"IRSIR1") && \
wcsicmp(pszStr, L"L2TP1") && \
wcsicmp(pszStr, L"PPTP1") && \
wcsicmp(pszStr, L"RNDISFN1") && \
wcsicmp(pszStr, L"WWAN1") && \
wcsicmp(pszStr, L"XSC1_IRDA1"))
{
LPSTR psStr;
PHB_ITEM pSubarray;
psStr = WideToAnsi( ( LPWSTR ) pszStr );
pSubarray = hb_itemPutC( NULL, psStr );
hb_arrayAddForward( pAdapters, pSubarray );
hb_itemRelease( pSubarray );
}
}
}
CloseHandle(hFile);
}
LeaveCriticalSection(&pWifi->m_Lock);
hb_itemReturnRelease( pAdapters );
}
BOOL RefreshBSSIDs( PFWIFIPEEK pWifi, LPWSTR pAdapter )
{
NDISUIO_SET_OID nso;
DWORD dwBytesRet;
BOOL retval;
EnterCriticalSection(&pWifi->m_Lock);
nso.ptcDeviceName = pAdapter;
nso.Oid = OID_802_11_BSSID_LIST_SCAN;
dwBytesRet=0;
if(!DeviceIoControl(pWifi->m_hNDUIO, IOCTL_NDISUIO_SET_OID_VALUE, (void *)&nso, sizeof(NDISUIO_SET_OID), NULL, 0, &dwBytesRet, NULL))
{
retval=FALSE;
}
else
{
retval=TRUE;
}
LeaveCriticalSection(&pWifi->m_Lock);
return retval;
}
HB_FUNC( REFRESHBSSIDS )
{
PFWIFIPEEK pWifi = ( PFWIFIPEEK ) hb_parnl( 1 );
LPSTR psStr = hb_parc( 2 );
LPWSTR pAdapter = AnsiToWide( psStr );
hb_retl( RefreshBSSIDs( pWifi, pAdapter ) );
hb_xfree( pAdapter );
}
//----------------------------------------------
char *MacToChar(char *pszMACAddress,
unsigned char *pbyMacAddressInBytes)
{
sprintf(pszMACAddress, "%02x%c%02x%c%02x%c%02x%c%02x%c%02x",
pbyMacAddressInBytes[0] & 0xff,
cSep,
pbyMacAddressInBytes[1]& 0xff,
cSep,
pbyMacAddressInBytes[2]& 0xff,
cSep,
pbyMacAddressInBytes[3]& 0xff,
cSep,
pbyMacAddressInBytes[4]& 0xff,
cSep,
pbyMacAddressInBytes[5]& 0xff);
return pszMACAddress;
}
CHAR * Get_Auth_Mode( int );
HB_FUNC( GETBBSIDS )
{
PNDISUIO_QUERY_OID pNQO;
DWORD dwBytesRet;
BYTE Buffer[8192], *pByte;
PNDIS_802_11_BSSID_LIST pList;
PNDIS_WLAN_BSSID pItem;
int i, iChannel;
PHB_ITEM pItemDest;
PHB_ITEM pitemField = hb_itemNew( NULL );
ULONG dwReturnedItems;
CHAR BSSID[64] = "";
PFWIFIPEEK pWifi = ( PFWIFIPEEK ) hb_parnl( 1 );
LPWSTR pAdapter = AnsiToWide( ( LPSTR ) hb_parc( 2 ) );
EnterCriticalSection(&pWifi->m_Lock);
pNQO=(PNDISUIO_QUERY_OID)Buffer;
pNQO->ptcDeviceName = pAdapter;
pNQO->Oid=OID_802_11_BSSID_LIST;
// Run query
dwBytesRet = 0;
if(!DeviceIoControl(pWifi->m_hNDUIO, IOCTL_NDISUIO_QUERY_OID_VALUE, (void *)pNQO, 8192, (void *)pNQO, 8192, &dwBytesRet, NULL))
{
LeaveCriticalSection(&pWifi->m_Lock);
hb_retl( FALSE );
}
pList=(PNDIS_802_11_BSSID_LIST)&pNQO->Data;
dwReturnedItems = pList->NumberOfItems;
pItemDest = hb_itemArrayNew( 0 );
//first item in array
pItem=pList->Bssid;
for(i=0; i<( INT )dwReturnedItems; i++)
{
MacToChar( BSSID, pItem->MacAddress );
if(pItem->Configuration.DSConfig > 14)
{
iChannel=(pItem->Configuration.DSConfig - 2407000) / 5000;
}else
{
iChannel=pItem->Configuration.DSConfig;
}
hb_arrayNew( pitemField, 6 );
hb_arraySetC( pitemField, 1, BSSID ); //MacAddress
hb_arraySetC( pitemField, 2, pItem->Ssid.Ssid ); // Ssid
hb_arraySetNI( pitemField, 3, iChannel ); // Channel
hb_arraySetC( pitemField, 4, sAuth_Mode[ pItem->Privacy ] ); // Auth
hb_arraySetC( pitemField, 5, sInfra_Mode[ pItem->InfrastructureMode ] ); //InfrastructureMode
hb_arraySetNI( pitemField, 6, pItem->Rssi ); //Signal
hb_arrayAddForward( pItemDest, pitemField );
//some pointer magic...actually pItem->Length was not sizeof(NDIS_WLAN_BSSID)
//so use returned length
pByte=(BYTE *)pItem;
pByte+=pItem->Length;
pItem=(PNDIS_WLAN_BSSID)pByte;
}//for
LeaveCriticalSection(&pWifi->m_Lock);
hb_itemRelease( pitemField );
hb_itemReturnRelease( pItemDest );
}