原文連接地址:https://blog.csdn.net/guoxiang3538/article/details/79266610算法
1 DWORD WINAPI 2 WlanOpenHandle( 3 __in DWORD dwClientVersion, 4 __reserved PVOID pReserved, 5 __out PDWORD pdwNegotiatedVersion, 6 __out PHANDLE phClientHandle 7 );
其取值以下:安全
1:Windows XP SP2網絡
2:Windows Vista and Windows Server 2008session
pReserved:保留值,設爲NULL架構
pdwNegotiatedVersion:Specifies the version of the WLAN API that will be used in this session(out值,返回當前使用的version,好比你在xpsp2下它就返回1)app
phClientHandle:Specifies a handle for the client to use in this session. This handle is used by other functions throughout the session(返回的客戶端句柄,用於其餘wifi函數調用)less
MSDN上表示:ide
WlanOpenHandle will return an error message if the Wireless Zero Configuration (WZC) service has not been started or if the WZC service is not responsive.(必須啓動WZC服務,否則WlanOpenHandle 返回失敗)函數
附上啓動WZC服務的方法:測試
1.開始菜單--運行--輸入services.msc 2.雙擊啓動Wireless Zero Configuration這個服務,點擊「啓動」3.改啓動類型爲自動,肯定
1 DWORD WINAPI 2 WlanEnumInterfaces( 3 __in HANDLE hClientHandle, 4 __reserved PVOID pReserved, 5 __deref_out PWLAN_INTERFACE_INFO_LIST *ppInterfaceList 6 );
hClientHandle:The client's session handle, obtained by a previous call to the WlanOpenHandle function.(WlanOpenHandle返回的那個客戶端句柄)
pReserved:保留值,設爲NULL
ppInterfaceList:Pointer to a WLAN_INTERFACE_INFO_LIST structure that contains the list of NIC interfaces.(網卡接口信息列表結構的指針,NIC:Network Interface Card,也就是網絡適配器/網卡),This function will allocate memory for the list of returned interfaces. The caller is responsible for freeing this memory using the WlanFreeMemory function(意思就是你傳一個空指針的地址進去就好了,系統自動分配,你記得用完後用WlanFreeMemory釋放)
1 typedef struct _WLAN_INTERFACE_INFO_LIST{ 2 DWORD dwNumberOfItems; 3 DWORD dwIndex; 4 WLAN_INTERFACE_INFO InterfaceInfo[];} WLAN_INTERFACE_INFO_LIST, *PWLAN_INTERFACE_INFO_LIST;
dwNumberOfItems:Contains the number of items in the InterfaceInfo member(InterfaceInfo[ ] 中包含的單元的個數)
dwIndex:0到dwNumberOfItems-1, 通常用於在 WLAN_INTERFACE_INFO_LIST 做爲參數傳遞時的一個傳遞偏移量。這個參數在用以前必需要進行初始化
InterfaceInfo[]:An array of WLAN_INTERFACE_INFO structures containing interface information.(包含WLAN_INTERFACE_INFO 結構體的陣列,用於記錄接口信息)
1 typedef struct _WLAN_INTERFACE_INFO { 2 GUID InterfaceGuid; 3 WCHAR strInterfaceDescription[256]; 4 WLAN_INTERFACE_STATE isState; 5 } WLAN_INTERFACE_INFO, *PWLAN_INTERFACE_INFO;
InterfaceGuid:Contains the GUID of the interface.(接口的GUID,GUID:Globally Unique Identifier(全球惟一標識符),據稱是根椐時間,網卡,機器名等結合算法生成的,因此惟一,因此調用者在某些函數中能夠經過GUID來指定特定網卡)
strInterfaceDescription[256]:Contains the description of the interface(接口的描繪信息,打開設備管理器-無線網卡屬性能夠看到描述)
isState:Contains a WLAN_INTERFACE_STATE value that indicates the current state of the interface.(包含一個 WLAN_INTERFACE_STATE值,標示這個NIC接口的當前狀態)
1 typedef enum _WLAN_INTERFACE_STATE { 2 wlan_interface_state_not_ready = 0, 3 wlan_interface_state_connected, 4 wlan_interface_state_ad_hoc_network_formed, 5 wlan_interface_state_disconnecting, 6 wlan_interface_state_disconnected, 7 wlan_interface_state_associating, 8 wlan_interface_state_discovering, 9 wlan_interface_state_authenticating 10 } WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE;
WirelessLAN API for Windows XP SP2:
Only the wlan_interface_state_connected, wlan_interface_state_disconnected, and wlan_interface_state_authenticating values are supported.(xpsp2下僅支持已鏈接,已斷開,生效中這三種狀態)
1 DWORD WINAPI WlanGetAvailableNetworkList( 2 __in HANDLE hClientHandle, 3 __in const GUID* pInterfaceGuid, 4 __in DWORD dwFlags, 5 PVOID pReserved, 6 __out PWLAN_AVAILABLE_NETWORK_LIST* ppAvailableNetworkList 7 );
hClientHandle:The client's session handle, obtained by a previous call to the WlanOpenHandle function.(WlanOpenHandle返回的那個客戶端句柄)
pInterfaceGuid:The GUID of the interface to be queried.(上面 WLAN_INTERFACE_INFO 的GUID,也就是網卡的GUID)
dwFlags:Controls the type of networks returned in the list(控制ppAvailableNetworkList中得到的網絡類型),其值爲0,1,2,3(1和2組合)四種選擇
1:Include all ad-hoc network profiles in the available network list, including profiles that are not visible.(ad-hoc network :即點對點方式的無線網絡,包括profiles name(配置文件名稱)不可見(得到profiles name字符爲空)的全部點對對無線網絡)
2:Include all hidden network profiles in the available network list, including profiles that are not visible.(包括profiles name(配置文件名稱)不可見(得到profiles name字符爲空)的全部隱藏網絡配置)
3:前兩者的組合
ppAvailableNetworkList:Pointer to a WLAN_AVAILABLE_NETWORK_LIST to receive the returned list of visible networks.(無線網絡列表)This function will allocate memory for the list of returned interfaces. The caller is responsible for freeing this memory using the WlanFreeMemory function(傳一個空指針的地址進去就好了,系統自動分配,調用者負責用WlanFreeMemory釋放)
1 typedef struct _WLAN_AVAILABLE_NETWORK_LIST { 2 DWORD dwNumberOfItems; 3 DWORD dwIndex; 4 WLAN_AVAILABLE_NETWORK Network[1]; 5 } WLAN_AVAILABLE_NETWORK_LIST, 6 *PWLAN_AVAILABLE_NETWORK_LIST;
dwNumberOfItems:Contains the number of items in the Network member(網絡數目)
dwIndex:0到dwNumberOfItems-1,通常用於在WLAN_AVAILABLE_NETWORK_LIST 做爲參數傳遞時的一個傳遞偏移量。這個參數在用以前必需要進行初始化
Network[1]:An array of WLAN_AVAILABLE_NETWORK structures containing interface information.(包含WLAN_AVAILABLE_NETWORK 的陣列,用於記錄網絡信息)
1 typedef struct _WLAN_AVAILABLE_NETWORK { 2 WCHAR strProfileName[256]; 3 DOT11_SSID dot11Ssid; 4 DOT11_BSS_TYPE dot11BssType; 5 ULONG uNumberOfBssids; 6 BOOL bNetworkConnectable; 7 WLAN_REASON_CODE wlanNotConnectableReason; 8 ULONG uNumberOfPhyTypes; 9 DOT11_PHY_TYPE dot11PhyTypes[WLAN_MAX_PHY_TYPE_NUMBER]; 10 BOOL bMorePhyTypes; 11 WLAN_SIGNAL_QUALITY wlanSignalQuality; 12 BOOL bSecurityEnabled; 13 DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm; 14 DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm; 15 DWORD dwFlags; 16 DWORD dwReserved; 17 } WLAN_AVAILABLE_NETWORK, *PWLAN_AVAILABLE_NETWORK;
strProfileName:Contains the profile name associated with the network. If the network doesn't have a profile, this member will be empty. If multiple profiles are associated with the network, there will be multiple entries with the same SSID in the visible network list. Profile names are case-sensitive. This string must be NULL-terminated(配置文件名,沒有就爲空,大小寫敏感)
dot11Ssid:A DOT11_SSID structure that contains the SSID of the visible wireless network(網絡的SSID,網絡名, SSID是Service Set Identifier的縮寫,意思是:服務集標識。SSID技術能夠將一個無線局域網分爲幾個須要不一樣身份驗證的子網絡,每個子網絡都須要獨立的身份驗證,只有經過身份驗證的用戶才能夠進入相應的子網絡,防止未被受權的用戶進入本網絡. 通俗地說,SSID即是你給本身的無線網絡所取的名字)
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 the network is infrastructure or ad hoc.(指明網絡是集中控制式仍是點對點式)
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來進行無線通信,設置後,無線網絡設備就必須有AP(Access Pointer)來溝通,通常有多個有線客戶端和無線客戶端圍繞一個AP或無線路由器組成的服務集,全部客戶端經過一個AP或無線路由器進行通訊,客戶端之間不直接進行通訊,與對等(Ad Hoc)無線網絡相比有更多的安全性和擴展能力
uNumberOfBssids: Indicates the number of BSSIDs in the network
ssid 是一個無線AP的名稱。bssid 是這個無線AP的MAC地址
bNetworkConnectable:Indicates whether the network is connectable or not(是否可鏈接)
wlanNotConnectableReason :indicates why a network cannot be connected to. This member is only valid when bNetworkConnectable is FALSE(若是網絡是不可鏈接的,這裏返回緣由)
wlanSignalQuality:A percentage value that represents the signal quality of the network(0-100網絡信號)
bSecurityEnabled:Indicates whether security is enabled on the network(有沒有安全鎖)
dot11DefaultAuthAlgorithm:A DOT11_AUTH_ALGORITHM value that indicates the default authentication algorithm used to join this network for the first time(首次加入該網絡使用的默認認證算法,我的理解是和無線網絡的安全認證算法(協議s)有關)
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的簡化版――WPA-PSK(預共享密鑰) 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 cipher algorithm to be used when joining this network(DOT11_CIPHER_ALGORITHM 值代表了加入這個網絡要使用的默認加密算法,我的理解是破解了這個加密算法後,咱就可免費蹭網了)
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,//TKIP由WEP使用的一樣的加密引擎和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;
1 DWORD WINAPI WlanConnect( 2 __in HANDLE hClientHandle, 3 __in const GUID* pInterfaceGuid, 4 __in const PWLAN_CONNECTION_PARAMETERS pConnectionParameters, 5 PVOID pReserved 6 );
hClientHandle:The client's session handle, returned by a previous call to the WlanOpenHandle function
pInterfaceGuid:The GUID of the interface to use for the connection.(網卡的GUID,不是要鏈接的網絡的ssid!)
pConnectionParameters:Pointer to a WLAN_CONNECTION_PARAMETERS structure that specifies the connection type, mode, network profile, SSID that identfies the network, and other parameters(簡言之,指出要鏈接的網絡)
1 typedef struct _WLAN_CONNECTION_PARAMETERS { 2 WLAN_CONNECTION_MODE wlanConnectionMode; 3 LPCWSTR strProfile; 4 PDOT11_SSID pDot11Ssid; 5 PDOT11_BSSID_LIST pDesiredBssidList; 6 DOT11_BSS_TYPE dot11BssType; 7 DWORD dwFlags; 8 } WLAN_CONNECTION_PARAMETERS, *PWLAN_CONNECTION_PARAMETERS;
wlanConnectionMode:A WLAN_CONNECTION_MODE value that specifies the mode of connection.Wireless LAN API for Windows XP SP2:Only the wlan_connection_mode_profile value is supported(鏈接模式,xpsp2下只能使用配置文件鏈接)
1 typedef enum _WLAN_CONNECTION_MODE { 2 wlan_connection_mode_profile = 0,//使用配置文件鏈接 3 wlan_connection_mode_temporary_profile,//使用臨時配置文件鏈接 4 wlan_connection_mode_discovery_secure,//使用發現的安全網絡鏈接 5 wlan_connection_mode_discovery_unsecure,//使用發現的不安全網絡鏈接 6 wlan_connection_mode_auto,//自動鏈接 7 wlan_connection_mode_invalid//...無效的 8 } WLAN_CONNECTION_MODE, *PWLAN_CONNECTION_MODE;
strProfile:Specifies 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 the connection. If wlanConnectionMode is set to wlan_connection_mode_temporary_profile, then strProfile specifies the XML representation of the profile used for the connection. If wlanConnectionMode is set to wlan_connection_mode_discovery_secure, wlan_connection_mode_discovery_unsecure, or wlan_connection_mode_auto, then strProfile should be set to NULL(配置文件名)
pDot11Ssid:pointer 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 or wlan_connection_mode_discovery_unsecure.(對應WLAN_AVAILABLE_NETWORK 中的DOT11_SSID ,如設爲NULL,配置文件的全部SSID都會被嘗試,若是爲wlan_connection_mode_discovery_secure 、wlan_connection_mode_discovery_unsecure不能設爲NULL)
pDesiredBssidList:Pointer to a DOT11_BSSID_LISTstructure that contains the list of basic service set (BSS) identifiers desired for the connection.Wireless LAN API for Windows XP SP2: This member must be NULL(xpsp2設爲NULL就好了)
dot11BssType:A DOT11_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.(對應WLAN_AVAILABLE_NETWORK的 DOT11_BSS_TYPE)
結合前四個步驟,掃描NIC,得到可鏈接網絡屬性,再鏈接,咱們能夠實現網絡屬性顯示和無密碼的網絡鏈接
下面介紹有密碼的網絡鏈接:
有密碼的網絡鏈接必須設置網絡配置文件(WlanSetProfile,WlanGetProfile)
將密碼寫入配置xml中,再調用WlanConnect,所以理解xml的元素是關鍵.
1 <?xml version="1.0"?> 2 <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1"> 3 <name>FAST_1797A2</name> 4 <SSIDConfig> 5 <SSID> 6 <hex>464153545F313739374132</hex> 7 <name>FAST_1797A2</name> 8 </SSID> 9 </SSIDConfig> 10 <connectionType>ESS</connectionType> 11 <MSM> 12 <security> 13 <authEncryption> 14 <authentication>WPA2PSK</authentication> 15 <encryption>AES</encryption> 16 <useOneX>false</useOneX> 17 </authEncryption> 18 <sharedKey> 19 <keyType>networkKey</keyType> 20 <protected>false</protected> 21 <keyMaterial>8C7AC8A03F13838846AA180B7F1B65A61D46FAB64743FFCA0E86F6EEB6DDC8AE</keyMaterial> 22 </sharedKey> 23 </security> 24 </MSM> 25 </WLANProfile>
通常要生成一個這樣的xml,咱們只需一個WLAN_AVAILABLE_NETWORK(前面ENUM的可用網絡結構體)
Xml 的namespace通常都是 http://www.microsoft.com/networking/WLAN/profile/v1
FIPSMode使用http://www.microsoft.com/networking/WLAN/profile/v2
<name>:wlan的名稱,和ssid名字同樣就好了(我的測試)
<SSIDConfig>:contains one or more SSIDs(包含SSID的域,但MSDN也有這樣的說明:
Windows XP with SP3 and Wireless LAN API for WindowsXP with SP2:At most one SSID element can appear in a profile.(因此僅能傳一個SSID元素進去就好了)
<SSID>:傳WLAN_AVAILABLE_NETWORK的ssid名字進去
<connectionType>:WLAN_AVAILABLE_NETWORK的DOT11_BSS_TYPE:ESS和IBSS
<connectionMode>:auto/manual, If connectionType is set to ESS, this value can be either auto or manual. The default value is auto if this element is absent.If connectionType is set to IBSS, this value must be manual.(若是connectionType爲ESS,可設自動或手動,默認爲自動,IBSS必須爲手動,上面XML爲沒有設置的ESS,默認爲自動)
<autoSwitch>:WindowsXP with SP3 and Wireless LAN API for WindowsXP with SP2:This element is not supported.
<authEncryption>:specifies the authentication and encryption pair to be used for this profile,指定了認證和加密element.
<authentication>:DOT11_AUTH_ALGORITHM之間存在如下對應關係:
1 tenum _DOT11_AUTH_ALGORITHM { 2 DOT11_AUTH_ALGO_80211_OPEN = 1, //open 3 DOT11_AUTH_ALGO_80211_SHARED_KEY = 2, //shared 4 DOT11_AUTH_ALGO_WPA = 3, //WPA 5 DOT11_AUTH_ALGO_WPA_PSK = 4, //WPAPSK 6 DOT11_AUTH_ALGO_WPA_NONE = 5, //WPA 7 DOT11_AUTH_ALGO_RSNA = 6, //WPA2 8 DOT11_AUTH_ALGO_RSNA_PSK = 7, //WPA2PSK 9 DOT11_AUTH_ALGO_IHV_START = 0x80000000, //open 10 DOT11_AUTH_ALGO_IHV_END = 0xffffffff //open 11 } DOT11_AUTH_ALGORITHM, * PDOT11_AUTH_ALGORITHM;
<encryption>:DOT11_CIPHER_ALGORITHM之間存在如下對應關係:
1 typedef enum _DOT11_CIPHER_ALGORITHM { 2 DOT11_CIPHER_ALGO_NONE = 0x00, //none 3 DOT11_CIPHER_ALGO_WEP40 = 0x01, //WEP 4 DOT11_CIPHER_ALGO_TKIP = 0x02, //TKIP 5 DOT11_CIPHER_ALGO_CCMP = 0x04, //AES 6 DOT11_CIPHER_ALGO_WEP104 = 0x05, //AES 7 DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100, //AES 8 DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100, //AES 9 DOT11_CIPHER_ALGO_WEP = 0x101, //WEP 10 DOT11_CIPHER_ALGO_IHV_START = 0x80000000, //none 11 DOT11_CIPHER_ALGO_IHV_END = 0xffffffff //none 12 } DOT11_CIPHER_ALGORITHM, * PDOT11_CIPHER_ALGORITHM;
The AES encryption method is as specified in the 802.1X and 802.11i specifications.
<keyType>:When the encryption element has a value of WEP, keyType must be set to networkKey.(當encryption是WEP時,必須爲networkKey,看了MSDN的profile,其他所有用passPhrase)
<protected>: For profiles intended for use on Windows XP with Service Pack 3 (SP3) or Wireless LAN API for Windows XP with Service Pack 2 (SP2), protectedmust have a value of FALSE. (必須設爲FALSE以支持xp)
<keyMaterial>:密碼明文放入便可,若是使用了加密算法,再取出來就是密文了
1 DWORD WINAPI WlanSetProfile( 2 __in HANDLE hClientHandle,//不解釋 3 __in const GUID* pInterfaceGuid, //不解釋 4 __in DWORD dwFlags,//0表示所有用戶 5 __in LPCWSTR strProfileXml,//前面的XML 6 __in_opt LPCWSTR strAllUserProfileSecurity,//xp下必須爲NULL 7 __in BOOL bOverwrite,//如已存在profile,是否覆蓋 8 __in PVOID pReserved,
1 DWORD WINAPI 2 WlanDisconnect( 3 __in HANDLE hClientHandle, //不解釋 4 __in CONST GUID *pInterfaceGuid, //不解釋 5 __reserved PVOID pReserved 6 );
xml元素我主要參考:http://msdn.microsoft.com/en-us/library/ms706965(v=vs.85).asp