Windows 扫描无线AP代码

调用Windows WLan API实现扫描无线AP。
编译环境:Visual Studio 2010 & Win7 SDK
// wLan.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
   
   
    
    
#include 
    
    
     
     

#pragma comment(lib, "Wlanapi.lib") 

bool bWait = true;

void WlanScanProc(WLAN_NOTIFICATION_DATA *NotifData, void *P)
{
    if (NotifData->NotificationCode == wlan_notification_acm_scan_complete){
		printf("ok!\n");
		bWait = false;
	}
	else if (NotifData->NotificationCode == wlan_notification_acm_scan_fail){
		printf("fail!\n");
		bWait = false;
	}
}

int wlan_connect(HANDLE hClient, GUID guid, char *ssid)
{
	PWLAN_CONNECTION_PARAMETERS pWlanConnPara;
	pWlanConnPara = (WLAN_CONNECTION_PARAMETERS *)WlanAllocateMemory(sizeof(WLAN_CONNECTION_PARAMETERS));

	pWlanConnPara->dot11BssType = dot11_BSS_type_any;
	pWlanConnPara->dwFlags = 0;
	pWlanConnPara->strProfile =L"E_SOP_AP";

	pWlanConnPara->wlanConnectionMode = wlan_connection_mode_profile;
	if (ERROR_SUCCESS == WlanConnect(hClient,&guid,pWlanConnPara,NULL))
		printf("Connect ok\n");
	return 0;
}


int wlan_search()
{
	char NicDesc[256];
	int i;
	DWORD ret;
	DWORD dwVer;
	GUID guid;
	DWORD dwPrevNotif = 0;
	HANDLE	hClient=NULL;
	PWLAN_INTERFACE_INFO_LIST pInterfaceList;
	PWLAN_AVAILABLE_NETWORK_LIST pWlanList;
	pWlanList = (WLAN_AVAILABLE_NETWORK_LIST *)WlanAllocateMemory(sizeof(WLAN_AVAILABLE_NETWORK_LIST));

	/* Open WlanDevice with one handle */
	if (ERROR_SUCCESS != WlanOpenHandle(WLAN_API_VERSION_1_0, NULL, &dwVer, &hClient)){
		printf("[-] Open NIC failed!\n");
		return 1;
	}

	/* Retieve WlanDevice interfaces */
	if (ERROR_SUCCESS == WlanEnumInterfaces(hClient, NULL, &pInterfaceList)){
		wcstombs(NicDesc, pInterfaceList->InterfaceInfo[0].strInterfaceDescription, sizeof(NicDesc));
		printf("\nDevice_Name=%s\n", NicDesc);
	}else{
		printf("[-] WLan device not found!\n");
		return 1;
	}
	/* retieve wlan device GUID */
	guid = pInterfaceList->InterfaceInfo[0].InterfaceGuid;

	if (pInterfaceList->InterfaceInfo[0].isState == wlan_interface_state_not_ready)
	{
		printf("[-] Interface is not ready!\n");
		return 1;
	}

	if (pInterfaceList->InterfaceInfo[0].isState == wlan_interface_state_disconnected)
	{
		printf("[-] Interface is disconnected!\n");
	}

	/* Register Notification with specified callback function */
	if (ERROR_SUCCESS != WlanRegisterNotification(hClient,
							WLAN_NOTIFICATION_SOURCE_ACM,
							true,
							( WLAN_NOTIFICATION_CALLBACK )WlanScanProc,
							NULL,
							NULL,
							&dwPrevNotif)){
		printf("[-] Register Callback failed!");
		return 1;
	}

	/* start scan wlan networks */
	if (ERROR_SUCCESS == WlanScan(hClient, &guid, NULL, NULL,NULL)){
		printf("Wlan scan sent ok\nWaiting for scan complete ... ");
	}

	/* waiting for Wlan Scan completed! */
	while(bWait)
		Sleep(500);

	/* cancel callback function of wlan scan */
	if (ERROR_SUCCESS == WlanRegisterNotification(hClient,
							WLAN_NOTIFICATION_SOURCE_ACM,
							true,
							NULL,
							NULL,
							NULL,
							&dwPrevNotif))
	{
		printf("[-] unregister callback failed!\n");
		WlanCloseHandle(hClient, NULL);
		WlanFreeMemory(pWlanList);
		return 1;
	}

	/* retieve networks list */
	if (ERROR_SUCCESS == WlanGetAvailableNetworkList(hClient, 
													&guid,
													WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES|WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES,
													NULL,
													&pWlanList)){
		printf("Retieving wlan list...\n");
	}

	printf("  SSID\t\tSignal\n");

	if (pWlanList->dwNumberOfItems<=0) {
		printf("[-] Network not found!");
		WlanCloseHandle(hClient, NULL);
		WlanFreeMemory(pWlanList);
		return 1;
	}

	for (i=0; i
     
     
      
      dwNumberOfItems; i++)
	{
		printf("%-15s\t %3d\n",
								pWlanList->Network[i].dot11Ssid.ucSSID,
								pWlanList->Network[i].wlanSignalQuality);
	}

//	wlan_connect(hClient,guid,"");
	
	WlanCloseHandle(hClient, NULL);
	WlanFreeMemory(pWlanList);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	 return wlan_search();
}


     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值