【WindowsCE 】WinCE WIFI 無線網絡卡的配置和建立連線
在 Windows CE 下自帶有無線網絡卡的配置和連線程式,這篇文章就來談一下如何用那麼可以利用微軟自帶的WZC函式來重新編寫一個獨立於系統的WIFI連線程式.
下面是一整套通過WZC函式連線無線網的程式,我自己測試後保證在系統下是可以執行.大家可以參考一下。
一、非常重要的庫檔案。
#include <eaputil.h>
#include <pm.h>
#include <wzcsapi.h>
#include <iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
#pragma comment(lib,"Wzcsapi.lib")
#pragma comment(lib,"cclib.lib")
二、列舉系統中可用的無線網路裝置
- BOOL GetFirstWirelessCard(PTCHAR pCard)
- {
- if (!pCard)
- {
- return FALSE;
- }
- INTFS_KEY_TABLE IntfsTable;
- IntfsTable.dwNumIntfs = 0;
- IntfsTable.pIntfs = NULL;
- _tcscpy(pCard, TEXT(""));
- // 列舉系統中可用的無線網絡卡
- DWORD dwStatus = WZCEnumInterfaces(NULL, &IntfsTable);
- if (dwStatus != ERROR_SUCCESS)
- {
- RETAILMSG(DBG_MSG, (TEXT("WZCEnumInterfaces() error 0x%08X\n"),dwStatus));
- return FALSE;
- }
- // 判斷無線網絡卡的數量,可以根據無線網絡卡數量來枚舉出所有可用的無線網絡卡
- if (!IntfsTable.dwNumIntfs)
- {
- RETAILMSG(DBG_MSG, (TEXT("System has no wireless card.\n"
- return FALSE;
- }
- _tcscpy(pCard, IntfsTable.pIntfs[0].wszGuid);
- LocalFree(IntfsTable.pIntfs);
- return TRUE;
- }
三、獲取無線網路資訊
獲取到了系統可用的無線網絡卡後,我們就可以利用它的 GUID 號來進行進一步的操作了,首先要做的事情就是得到該無線網絡卡的資訊以及該無線網絡卡掃描到的 WIFI 閘道器資訊。
以下函式可以獲取到該無線網絡卡及掃描的到的無線 AP 資訊
- //////////////////////////////////////////////////////////////////////////
- // pCard: 無線網絡卡 GUID
- // pIntf: 無線網絡卡配置資訊結果體
- // pOutFlags: 網絡卡配置資訊掩碼標誌
- //////////////////////////////////////////////////////////////////////////
- BOOL GetWirelessCardInfo(PTCHAR pCard, PINTF_ENTRY_EX pIntf, PDWORD pOutFlags)
- {
- TCHAR *szWiFiCard = NULL;
- // 引數校驗
- if (!pCard || !pIntf || !pOutFlags)
- {
- RETAILMSG(DBG_MSG, (TEXT("Param Error.\n")));
- return FALSE;
- }
- szWiFiCard = pCard;
- *pOutFlags = 0;
- // 初始化無線網絡卡資訊
- ZeroMemory(pIntf, sizeof(INTF_ENTRY_EX));
- // 設定 GUID 號
- pIntf->wszGuid = szWiFiCard;
- // 查詢無線網絡卡資訊
- DWORD dwStatus = WZCQueryInterfaceEx(NULL, INTF_ALL, pIntf, pOutFlags);
- if (dwStatus != ERROR_SUCCESS)
- {
- RETAILMSG(DBG_MSG, (TEXT("WZCQueryInterfaceEx() error 0x%08X\n"), dwStatus));
- return FALSE;
- }
- return TRUE;
- }
四、判斷連線狀態
我們可以通過無線網絡卡的狀態來判斷當前無線網絡卡是否已經和無線AP建立了連線
- BOOL IsAssociated(const INTF_ENTRY_EX Intf, const DWORD dwOutFlags)
- {
- if (dwOutFlags & INTF_BSSID)
- {
- PRAW_DATA prdMAC = (PRAW_DATA)(&Intf.rdBSSID);
- // 判斷 BSSID 的 MAC 地址是否有效來判斷是否和無線AP建立了連線
- if (prdMAC == NULL || prdMAC->dwDataLen == 0 ||
- (!prdMAC->pData[0] && !prdMAC->pData[1] && !prdMAC->pData[2] &
- !prdMAC->pData[3] && !prdMAC->pData[4] && !prdMAC->pData[5]))
- {
- RETAILMSG(DBG_MSG, (TEXT("(This wifi card is not associated to any)\n")));
- return FALSE;
- }
- else
- {
- RETAILMSG(DBG_MSG, (TEXT("(This wifi card is associated state)\n")));
- return TRUE;
- }
- }
- else
- {
- return FALSE;
- }
- }
五、獲取無線AP資訊
獲取了無線網絡卡的資訊後,可以通過無線網絡卡枚舉出當前所有可用的無線AP的SSID名稱以及加密模式等等所有可用資訊,一下函式可以實現該功能
- void GetWirelseeListSSID(const PRAW_DATA prdBSSIDList, HWND hListCtlWnd)
- {
- if (prdBSSIDList == NULL || prdBSSIDList->dwDataLen == 0)
- {
- RETAILMSG(DBG_MSG, (TEXT("<null> entry.\n")));
- }
- else
- {
- PWZC_802_11_CONFIG_LIST pConfigList = (PWZC_802_11_CONFIG_LIST)prdBSSIDList->pData;
- //RETAILMSG(DBG_MSG, (TEXT("[%d] entries.\n"), pConfigList->NumberOfItems));
- uint i;
- // 列舉所有無線AP
- for (i = 0; i < pConfigList->NumberOfItems; i++)
- {
- PWZC_WLAN_CONFIG pConfig = &(pConfigList->Config[i]);
- RAW_DATA rdBuffer;
- rdBuffer.dwDataLen = pConfig->Ssid.SsidLength;
- rdBuffer.pData = pConfig->Ssid.Ssid;
- TCHAR tSsid[MAX_PATH];
- // 將 SSID 的 ASCII 碼轉化成字串
- PrintSSID(&rdBuffer, tSsid);
- if (hListCtlWnd)
- {
- if (ListBox_FindString(hListCtlWnd, 0, tSsid) == LB_ERR)
- {
- ListBox_AddString(hListCtlWnd, tSsid);
- }
- }
- //RETAILMSG(DBG_MSG, (TEXT("\n")));
- }
- }
- }
六、連線到指定的無線AP
- //////////////////////////////////////////////////////////////////////////
- // pCard: 無線網絡卡 GUID
- // pSSID: 無線AP SSID號
- // bAdhoc: 是否點對點的 WIFI 連線
- // ulPrivacy: 加密模式(WEP/WPA....)
- // ndisMode: 認證模式(Open/Share)
- // iKeyIndex: 金鑰索引(1-4)
- // pKey: 密碼
- // iEapType: 802.11 認證模式
- //////////////////////////////////////////////////////////////////////////
- BOOL WirelessConnect(PTCHAR pCard, PTCHAR pSSID, BOOL bAdhoc, ULONG ulPrivacy, NDIS_802_11_AUTHENTICATION_MODE ndisMode, int iKeyIndex, PTCHAR pKey, int iEapType)
- {
- BOOL bRet = FALSE;
- if (!pSSID)
- {
- RETAILMSG(DBG_MSG, (TEXT("Param Error.\n")));
- return FALSE;
- }
- else
- {
- WZC_WLAN_CONFIG wzcConfig;
- ZeroMemory(&wzcConfig, sizeof(WZC_WLAN_CONFIG));
- wzcConfig.Length = sizeof(WZC_WLAN_CONFIG);
- wzcConfig.dwCtlFlags = 0;
- wzcConfig.Ssid.SsidLength = _tcslen(pSSID);
- for (UINT i = 0; i < wzcConfig.Ssid.SsidLength; i++)
- {
- wzcConfig.Ssid.Ssid[i] = (CHAR)pSSID[i];
- }
- if (bAdhoc)
- {
- wzcConfig.InfrastructureMode = Ndis802_11IBSS;
- }
- else
- {
- wzcConfig.InfrastructureMode = Ndis802_11Infrastructure;
- }
- wzcConfig.AuthenticationMode = ndisMode;
- wzcConfig.Privacy = ulPrivacy;
- if (pKey == NULL || _tcslen(pKey) == 0)
- {
- // 對金鑰進行轉換
- bRet = InterpretEncryptionKeyValue(wzcConfig, 0, NULL, TRUE);
- wzcConfig.EapolParams.dwEapType = iEapType;
- wzcConfig.EapolParams.dwEapFlags = EAPOL_ENABLED;
- wzcConfig.EapolParams.bEnable8021x = TRUE;
- wzcConfig.EapolParams.dwAuthDataLen = 0;
- wzcConfig.EapolParams.pbAuthData = 0;
- }
- else
- {
- RETAILMSG(DBG_MSG, (TEXT("WirelessConnect iKeyIndex = %d.\n"), iKeyIndex));
- bRet = InterpretEncryptionKeyValue(wzcConfig, iKeyIndex, pKey, FALSE);
- }
- // 連線到指定的無線AP,並將該AP新增到首先無線AP中
- AddToPreferredNetworkList(pCard, wzcConfig, pSSID);
- }
- return bRet;
- }
七、金鑰轉換
輸入的金鑰需要通過加密方式進行一定的轉化,以下函式可以完成改功能
- static void EncryptWepKMaterial(IN OUT WZC_WLAN_CONFIG* pwzcConfig)
- {
- BYTE chFakeKeyMaterial[] = { 0x56, 0x09, 0x08, 0x98, 0x4D, 0x08, 0x11, 0x66, 0x42, 0x03, 0x01, 0x67, 0x66 };
- for (int i = 0; i < WZCCTL_MAX_WEPK_MATERIAL; i++)
- pwzcConfig->KeyMaterial[i] ^= chFakeKeyMaterial[(7*i)%13];
- }
- BOOL InterpretEncryptionKeyValue(IN OUT WZC_WLAN_CONFIG& wzcConfig, IN int iKeyIndex, IN PTCHAR pKey, IN BOOL bNeed8021X)
- {
- if(wzcConfig.Privacy == Ndis802_11WEPEnabled)
- {
- if(!bNeed8021X && pKey)
- {
- wzcConfig.KeyIndex = iKeyIndex;
- wzcConfig.KeyLength = _tcslen(pKey);
- if((wzcConfig.KeyLength == 5) || (wzcConfig.KeyLength == 13))
- {
- for(UINT i=0; i<wzcConfig.KeyLength; i++)
- wzcConfig.KeyMaterial[i] = (UCHAR)pKey[i];
- }
- else
- {
- if((pKey[0] != TEXT('0')) || (pKey[1] != TEXT('x')))
- {
- RETAILMSG(DBG_MSG, (TEXT("Invalid key value.\n")));
- return FALSE;
- }
- pKey += 2;
- wzcConfig.KeyLength = wcslen(pKey);
- if((wzcConfig.KeyLength != 10) && (wzcConfig.KeyLength != 26))
- {
- RETAILMSG(DBG_MSG, (TEXT("Invalid key value.\n")));
- return FALSE;
- }
- wzcConfig.KeyLength >>= 1;
- for(UINT i=0; i<wzcConfig.KeyLength; i++)
- {
- wzcConfig.KeyMaterial[i] = (HEX(pKey[2 * i]) << 4) | HEX(pKey[2 * i + 1]);
- }
- }
- EncryptWepKMaterial(&wzcConfig);
- wzcConfig.dwCtlFlags |= WZCCTL_WEPK_PRESENT;
- }
- }
- else if(wzcConfig.Privacy == Ndis802_11Encryption2Enabled
- || wzcConfig.Privacy == Ndis802_11Encryption3Enabled)
- {
- if(!bNeed8021X)
- {
- wzcConfig.KeyLength = wcslen(pKey);
- if((wzcConfig.KeyLength < 8) || (wzcConfig.KeyLength > 63))
- {
- RETAILMSG(DBG_MSG, (TEXT("WPA-PSK/TKIP key should be 8-63 char long string.\n")));
- return FALSE;
- }
- char szEncryptionKeyValue8[64]; // longest key is 63
- memset(szEncryptionKeyValue8, 0, sizeof(szEncryptionKeyValue8));
- WideCharToMultiByte(CP_ACP,
- 0,
- pKey,
- wzcConfig.KeyLength + 1,
- szEncryptionKeyValue8,
- wzcConfig.KeyLength + 1,
- NULL,
- NULL);
- WZCPassword2Key(&wzcConfig, szEncryptionKeyValue8);
- EncryptWepKMaterial(&wzcConfig);
- wzcConfig.dwCtlFlags |= WZCCTL_WEPK_XFORMAT
- | WZCCTL_WEPK_PRESENT
- | WZCCTL_ONEX_ENABLED;
- }
- wzcConfig.EapolParams.dwEapFlags = EAPOL_ENABLED;
- wzcConfig.EapolParams.dwEapType = DEFAULT_EAP_TYPE;
- wzcConfig.EapolParams.bEnable8021x = TRUE;
- wzcConfig.WPAMCastCipher = Ndis802_11Encryption2Enabled;
- }
- return TRUE;
- }
通過以上操作,完全可以連線到可用的無線AP了。