native wifi api 详解

来源:互联网 发布:广告公司记账源码 编辑:程序博客网 时间:2024/06/05 01:05

对于windows环境下的wifi API函数主要分两种:

1.      Wlan开头的函数:对XP,win7的系统开发环境,windowServer 2008

2.      WFD开头的函数: Windows 8 and Windows Server 2012

 

本次就一种环境下的API讲解:

1、  首先打开Wlan设备,及是设备资源句柄:

DWORD WINAPIWlanOpenHandle(

  _In_      DWORD   dwClientVersion,

  _Reserved_ PVOID   pReserved,

  _Out_     PDWORD  pdwNegotiatedVersion,

  _Out_     PHANDLE phClientHandle

);

dwClientVersion:对于windows XP 的SP2或者SP3版本,取值为 1

                                      对于Windows Vista 和WindowsServer 2008 取值为 2

pReserved:取值为NULL

pdwNegotiatedVersion [out]:返回参数

phClientHandle [out]:wifi设备的句柄,在整个扫描或者连接wifi过程中都会被用到。

Return value:成功返回ERROR_SUCCESS,错误会返回错误代码:

2、  扫描wifi卡的个数,因为有可能驱动管理这过个wifi卡设备

DWORD WINAPIWlanEnumInterfaces(

  _In_      HANDLE                   hClientHandle,

  _Reserved_ PVOID                     pReserved,

  _Out_     PWLAN_INTERFACE_INFO_LIST*ppInterfaceList

);

hClientHandle [in]:通过WlanOpenHandle获取的wifi设备资源的句柄(phClientHandle)。

pReserved [in]:NULL

ppInterfaceList [out]:指针结构体,它是存储wireless LANinterfaces 的一个结构体

Return value:成功返回ERROR_SUCCESS,错误会返回错误代码:

细说一下WLAN_INTERFACE_INFO_LIST结构体:

typedef struct _WLAN_INTERFACE_INFO_LIST {

  DWORD              dwNumberOfItems;

 DWORD              dwIndex;

 WLAN_INTERFACE_INFO InterfaceInfo[];

} WLAN_INTERFACE_INFO_LIST,*PWLAN_INTERFACE_INFO_LIST;

dwNumberOfItems:表示有多少个网卡,有多个要对多个网卡接口进行处理。

dwIndex:表示当前在使用哪个网卡,序号从0开始。

InterfaceInfo:一维数组,包含着wifi接口的信息。

细说一下WLAN_INTERFACE_INFO结构体:
typedef struct _WLAN_INTERFACE_INFO {
  GUID                 InterfaceGuid;
  WCHAR                strInterfaceDescription[256];
  WLAN_INTERFACE_STATE isState;
} WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;

InterfaceGuid:表示wifi接口的GUID的唯一标识符。

strInterfaceDescription:接口的描述符。

isStatewifi接口的状态,如下:

typedef enum _WLAN_INTERFACE_STATE { 
  wlan_interface_state_not_ready              = 0,
  wlan_interface_state_connected              = 1,
  wlan_interface_state_ad_hoc_network_formed  = 2,
  wlan_interface_state_disconnecting          = 3,
  wlan_interface_state_disconnected           = 4,
  wlan_interface_state_associating            = 5,
  wlan_interface_state_discovering            = 6,
  wlan_interface_state_authenticating         = 7
} WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE;
3、 扫描逐个网卡,获取周围有用的wifi网络:
DWORD WINAPI WlanGetAvailableNetworkList(
  _In_             HANDLE                       hClientHandle,
  _In_       const GUID                         *pInterfaceGuid,
  _In_             DWORD                        dwFlags,
  _Reserved_       PVOID                        pReserved,
  _Out_            PWLAN_AVAILABLE_NETWORK_LIST *ppAvailableNetworkList
);
hClientHandle [in]:通过WlanOpenHandle获取的wifi设备资源的句柄(phClientHandle)。
pInterfaceGuid [in]通过WlanEnumInterfaces获取WLAN_INTERFACE_INFO_LIST下的InterfaceInfo及是wifi接口的GUID的唯一标识符。
dwFlags [in]:1是所有可用的wifi,2还是隐藏的wifi
pReservedNULL
ppAvailableNetworkList [out]指针结构体,它是存储WLAN_AVAILABLE_NETWORK_LIST的一个结构体

Return value:成功返回ERROR_SUCCESS,错误会返回错误代码:

细说一下WLAN_INTERFACE_INFO结构体:
typedef struct _WLAN_AVAILABLE_NETWORK_LIST {
  DWORD                  dwNumberOfItems;
  DWORD                  dwIndex;
  WLAN_AVAILABLE_NETWORK Network[1];
} WLAN_AVAILABLE_NETWORK_LIST, *PWLAN_AVAILABLE_NETWORK_LIST;
dwNumberOfItems:表示周围可用的ap个数
dwIndex:表示当前用的是第几个ap
Network:一维数组,表示当前ap的详细信息。AP的详细信息如下:
typedef struct _WLAN_AVAILABLE_NETWORK {
  WCHAR                  strProfileName[256];
  DOT11_SSID             dot11Ssid;
  DOT11_BSS_TYPE         dot11BssType;
  ULONG                  uNumberOfBssids;
  BOOL                   bNetworkConnectable;
  WLAN_REASON_CODE       wlanNotConnectableReason;
  ULONG                  uNumberOfPhyTypes;
  DOT11_PHY_TYPE         dot11PhyTypes[WLAN_MAX_PHY_TYPE_NUMBER];
  BOOL                   bMorePhyTypes;
  WLAN_SIGNAL_QUALITY    wlanSignalQuality;
  BOOL                   bSecurityEnabled;
  DOT11_AUTH_ALGORITHM   dot11DefaultAuthAlgorithm;
  DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
  DWORD                  dwFlags;
  DWORD                  dwReserved;
} WLAN_AVAILABLE_NETWORK, *PWLAN_AVAILABLE_NETWORK;
这是数字的详细信息请看:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms707403(v=vs.85).aspx

strProfileName:Contains the profile name associated with the network. If the networkdoesn't have a profile, this member will be empty. If multiple profiles areassociated with the network, there will be multiple entries with the same SSIDin the visible network list. Profile names are case-sensitive. This string mustbe NULL-terminated(配置文件名,没有就为空,大小写敏感)

dot11Ssid:A DOT11_SSID structure that contains the SSID of thevisible wireless network(网络的SSID,网络名, SSIDService Set Identifier的缩写,意思是:服务集标识。SSID技术可以将一个无线局域网分为几个需要不同身份验证的子网络,每一个子网络都需要独立的身份验证,只有通过身份验证的用户才可以进入相应的子网络,防止未被授权的用户进入本网络.通俗地说,SSID便是你给自己的无线网络所取的名字)

[cpp] view plaincopy

1.  typedef struct _DOT11_SSID {  

2.   ULONG uSSIDLength;  

3.    UCHAR ucSSID[DOT11_SSID_MAX_LENGTH];  

4. } DOT11_SSID,   

5.   *PDOT11_SSID;  

uSSIDLength:实际长度(byte单位)

ucSSID:SSID字符串,注意typedef unsigned char UCHAR;所以在Unicode环境下,要做字符转换再打印显示

dot11BssType:A DOT11_BSS_TYPE value that specifies whether thenetwork is infrastructure or ad hoc.(指明网络是集中控制式还是点对点式)

[cpp] view plaincopy

1.  typedef enum _DOT11_BSS_TYPE{  

2.   

3.  dot11_BSS_type_infrastructure//BSS  

4.   

5.  dot11_BSS_type_infrastructure//IBSS  

6.   

7.  dot11_BSS_type_any//other  

8.   

9.  }DOT11_BSS_TYPE, *PDOT11_BSS_TYPE;  

集中控制式(Infrastructure)也称独立无线网络,简称BSS,是在一种整合有线与无线局域网架构的应用模式,与ad- hoc不同的是配备无线网卡的电脑必须通过ap来进行无线通讯,设置后,无线网络设备就必须有APAccess Pointer)来沟通,一般有多个有线客户端和无线客户端围绕一个AP或无线路由器组成的服务集,所有客户端通过一个AP或无线路由器进行通信,客户端之间不直接进行通信,与对等(Ad Hoc)无线网络相比有更多的安全性和扩展能力

uNumberOfBssids: Indicates the number of BSSIDs in the network

ssid 是一个无线AP的名称。bssid是这个无线APMAC地址

bNetworkConnectable:Indicates whether the network is connectable or not(是否可连接)

wlanNotConnectableReason :indicates why a network cannot be connected to. This member isonly valid when bNetworkConnectable is FALSE(如果网络是不可连接的,这里返回原因)

wlanSignalQuality:A percentage value that represents the signal quality of thenetwork(0-100网络信号)

bSecurityEnabled:Indicates whether security is enabled on the network(有没有安全锁)

dot11DefaultAuthAlgorithm:A DOT11_AUTH_ALGORITHM value that indicates the defaultauthentication algorithm used to join this network for the first time(首次加入该网络使用的默认认证算法,个人理解是和无线网络的安全认证算法(协议s)有关)

[cpp] view plaincopy

1.  typedef enum _DOT11_AUTH_ALGORITHM {  

2.     DOT11_AUTH_ALGO_80211_OPEN = 1,//80211开放系统认证算法  

3.      DOT11_AUTH_ALGO_80211_SHARED_KEY = 2,//80211共享密钥认证算法  

4.     DOT11_AUTH_ALGO_WPA = 3,//wifi保护访问(WPA)  

5.      DOT11_AUTH_ALGO_WPA_PSK = 4,//WPA的简化版――WPAPSK(预共享密钥)  

6.     DOT11_AUTH_ALGO_WPA_NONE = 5,                   DOT11_AUTH_ALGO_RSNA = 6,  

7.      DOT11_AUTH_ALGO_RSNA_PSK = 7,  

8.     DOT11_AUTH_ALGO_IHV_START = 0x80000000,  

9.      DOT11_AUTH_ALGO_IHV_END = 0xffffffff  

10.} DOT11_AUTH_ALGORITHM, * PDOT11_AUTH_ALGORITHM;  

dot11DefaultCipherAlgorithm:A DOT11_CIPHER_ALGORITHM value that indicates the default cipheralgorithm to be used when joining this network(DOT11_CIPHER_ALGORITHM 值表明了加入这个网络要使用的默认加密算法,个人理解是破解了这个加密算法后,咱就可免费蹭网了)

[cpp] view plaincopy

1.  typedef enum _DOT11_CIPHER_ALGORITHM {  

2.     DOT11_CIPHER_ALGO_NONE = 0x00,//无加密算法可用或支持  

3.      DOT11_CIPHER_ALGO_WEP40 = 0x01,//有线对等协议(WEP)算法  

4.     DOT11_CIPHER_ALGO_TKIP = 0x02,//    DOT11_CIPHER_ALGO_CCMP = 0x04,//TKIPWEP使用的同样的加密引擎和RC4算法组成  

5.      DOT11_CIPHER_ALGO_WEP104 = 0x05,  

6.     DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100,  

7.      DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100,  

8.     DOT11_CIPHER_ALGO_WEP = 0x101,  

9.      DOT11_CIPHER_ALGO_IHV_START = 0x80000000,  

10.    DOT11_CIPHER_ALGO_IHV_END = 0xffffffff  

11. } DOT11_CIPHER_ALGORITHM, * PDOT11_CIPHER_ALGORITHM;  

 
4、 链接wifi
DWORD WINAPI WlanConnect(
  _In_             HANDLE                      hClientHandle,
  _In_       const GUID                        *pInterfaceGuid,
  _In_       const PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
  _Reserved_       PVOID                       pReserved
);
hClientHandle [in]通过WlanOpenHandle获取的wifi设备资源的句柄(phClientHandle)。
pInterfaceGuid [in]通过WlanEnumInterfaces获取WLAN_INTERFACE_INFO_LIST下的InterfaceInfo及是wifi接口的GUID的唯一标识符。
pConnectionParameters [in]Pointer to a WLAN_CONNECTION_PARAMETERS structure that specifies the connection type, mode, network profile, SSID that identifies the network, and other parameters.
pReservedNULL

Return value:成功返回ERROR_SUCCESS,错误会返回错误代码:

细说一下WLAN_CONNECTION_PARAMETERS

typedef struct _WLAN_CONNECTION_PARAMETERS {

  WLAN_CONNECTION_MODEwlanConnectionMode;

 LPCWSTR             strProfile;

 PDOT11_SSID         pDot11Ssid;

 PDOT11_BSSID_LIST    pDesiredBssidList;

 DOT11_BSS_TYPE       dot11BssType;

 DWORD               dwFlags;

} WLAN_CONNECTION_PARAMETERS, *PWLAN_CONNECTION_PARAMETERS;

wlanConnectionModeWLAN_CONNECTION_MODE value that specifies the mode of connection.Windows XP SP3SP2下只支持wlan_connection_mode_profile

strProfileSpecifies the profile being used for the connection.

If wlanConnectionMode is set to wlan_connection_mode_profile,then strProfile specifies the name of the profile used for theconnection. If wlanConnectionMode is set towlan_connection_mode_temporary_profile,then strProfile specifies the XML representation of theprofile used for the connection. If wlanConnectionMode is set towlan_connection_mode_discovery_secure or wlan_connection_mode_discovery_unsecure,thenstrProfile should be set to NULL.

Windows XP with SP3 and Wireless LANAPI for Windows XP with SP2:  The profile mustmeet the compatibility criteria described in Wireless Profile Compatibility.

pDot11SsidPointer to a DOT11_SSID structure that specifies the SSID of the network to connect to. This parameter is optional. When set to NULL, all SSIDs in the profile will be tried. This parameter must not be NULL ifWLAN_CONNECTION_MODE is set to wlan_connection_mode_discovery_secure orwlan_connection_mode_discovery_unsecure.

pDesiredBssidListPointer to a DOT11_BSSID_LIST structure thatcontains the list of basic service set (BSS) identifiers desired for theconnection.

Windows XP with SP3 and Wireless LANAPI for Windows XP with SP2:  This member mustbeNULL.

dot11BssTypeDOT11_BSS_TYPE value that indicates the BSS type of the network. If a profile is provided, this BSS type must be the same as the one in the profile.

dwFlags

The followingtable shows flags used to specify the connection parameters.

最后附上官网程序:

 
0 0