native wifi api使用方法

本文展示了一个使用Wlanapi库枚举系统中所有无线网络接口的C/C++示例程序。通过调用WlanOpenHandle和WlanEnumInterfaces等API,程序能够获取并打印每个接口的索引、GUID、描述及状态。
摘要由CSDN通过智能技术生成
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <wlanapi.h>
#include <objbase.h>
#include <wtypes.h>

#include <stdio.h>
#include <stdlib.h>

#include <conio.h>

// Need to link with Wlanapi.lib and Ole32.lib
#pragma comment(lib, "wlanapi.lib")
#pragma comment(lib, "ole32.lib")

int wmain()
{

// Declare and initialize variables.

HANDLE hClient = NULL;
DWORD dwMaxClient = 2;   //    
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
int iRet = 0;

WCHAR GuidString[40] = {0};

int i;

/* variables used for WlanEnumInterfaces  */

PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;


dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); 
if (dwResult != ERROR_SUCCESS)  {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
// FormatMessage can be used to find out why the function failed
return 1;
}

dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); 
if (dwResult != ERROR_SUCCESS)  {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
// FormatMessage can be used to find out why the function failed
return 1;
}
else {
wprintf(L"Num Entries: %lu\n", pIfList->dwNumberOfItems);
wprintf(L"Current Index: %lu\n", pIfList->dwIndex);
for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[i];
wprintf(L"  Interface Index[%d]:\t %lu\n", i, i);
iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, 39); 
// For c rather than C++ source code, the above line needs to be
// iRet = StringFromGUID2(&pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString, 39); 
if (iRet == 0)
wprintf(L"StringFromGUID2 failed\n");
else {
wprintf(L"  InterfaceGUID[%d]: %ws\n",i, GuidString);
}    
wprintf(L"  Interface Description[%d]: %ws", i, 
pIfInfo->strInterfaceDescription);
wprintf(L"\n");
wprintf(L"  Interface State[%d]:\t ", i);
switch (pIfInfo->isState) {
case wlan_interface_state_not_ready:
wprintf(L"Not ready\n");
break;
case wlan_interface_state_connected:
wprintf(L"Connected\n");
break;
case wlan_interface_state_ad_hoc_network_formed:
wprintf(L"First node in a ad hoc network\n");
break;
case wlan_interface_state_disconnecting:
wprintf(L"Disconnecting\n");
break;
case wlan_interface_state_disconnected:
wprintf(L"Not connected\n");
break;
case wlan_interface_state_associating:
wprintf(L"Attempting to associate with a network\n");
break;
case wlan_interface_state_discovering:
wprintf(L"Auto configuration is discovering settings for the network\n");
break;
case wlan_interface_state_authenticating:
wprintf(L"In process of authenticating\n");
break;
default:
wprintf(L"Unknown state %ld\n", pIfInfo->isState);
break;
}
wprintf(L"\n");
}
}

if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}

_getch();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值