1. 查询SSID是否存在
bool Isfindssid(WCHAR *pssid)
{
HANDLE hClient = NULL;
DWORD dwMaxClient = 2; //
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
int iRet = 0;
WCHAR GuidString[39] = { 0 };
unsigned int i,j,k;
WCHAR szssid[256];
BOOL bSearchflag = FALSE;
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
int iRSSI = 0;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
return false;
// You can use FormatMessage here to find out why the function failed
}
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
return false;
// You can use FormatMessage here to find out why the function failed
}
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[%u]:\t %lu\n", i, i);
iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
sizeof(GuidString) / sizeof(*GuidString));
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");
dwResult = WlanGetAvailableNetworkList(hClient,
&pIfInfo->InterfaceGuid,
0,
NULL,
&pBssList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanGetAvailableNetworkList failed with error: %u\n",
dwResult);
// You can use FormatMessage to find out why the function failed
}
else {
wprintf(L"WLAN_AVAILABLE_NETWORK_LIST for this interface\n");
wprintf(L" Num Entries: %lu\n\n", pBssList->dwNumberOfItems);
for (j = 0; j < pBssList->dwNumberOfItems; j++) {
memset(szssid, 0, 256);
pBssEntry = (WLAN_AVAILABLE_NETWORK *)& pBssList->Network[j];
if (pBssEntry->dot11Ssid.uSSIDLength == 0)
wprintf(L"\n");
else {
for (k = 0; k < pBssEntry->dot11Ssid.uSSIDLength; k++) {
//wprintf(L"%c", (int)pBssEntry->dot11Ssid.ucSSID[k]);
szssid[k] = (int)pBssEntry->dot11Ssid.ucSSID[k];
}
if (wcscmp(szssid, pssid) == 0)
{
wprintf(L" Profile Name[%u]: %ws\n", j, pBssEntry->strProfileName);
wprintf(L" SSID[%u]:\t\t ", j);
wprintf(L"%s\n", szssid);
wprintf(L" Connectable[%u]:\t ", j);
if (pBssEntry->bNetworkConnectable)
wprintf(L"Yes\n");
else {
wprintf(L"No\n");
wprintf(L" Not connectable WLAN_REASON_CODE value[%u]:\t %u\n", j,
pBssEntry->wlanNotConnectableReason);
}
if (pBssEntry->wlanSignalQuality == 0)
iRSSI = -100;
else if (pBssEntry->wlanSignalQuality == 100)
iRSSI = -50;
else
iRSSI = -100 + (pBssEntry->wlanSignalQuality / 2);
wprintf(L" Signal Quality[%u]:\t %u (RSSI: %i dBm)\n", j,
pBssEntry->wlanSignalQuality, iRSSI);
bSearchflag = TRUE;
break;
}
}
}
}
}
}
if (pBssList != NULL) {
WlanFreeMemory(pBssList);
pBssList = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
if (bSearchflag){
return true;
}else{
return false;
}
}
2. 枚举无线网卡的状态
bool ShowWlanStatus()
{
HANDLE hClient = NULL;
DWORD dwMaxClient = 2; //
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
int iRet = 0;
WCHAR GuidString[39] = { 0 };
unsigned int i;
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
int iRSSI = 0;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
return false;
// You can use FormatMessage here to find out why the function failed
}
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
return false;
// You can use FormatMessage here to find out why the function failed
}
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[%u]:\t %lu\n", i, i);
iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
sizeof(GuidString) / sizeof(*GuidString));
// For c rather than C++ source code, the above line needs to be
// iRet = StringFromGUID2(&pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString,
// sizeof(GuidString)/sizeof(*GuidString));
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;
}
return true;
}
3. 连接无线网络
//仅考虑了一种网卡的情况,多种网卡不support
BOOL ConnectSSID(CString strssid)
{
HANDLE hClient = NULL;
DWORD dwMaxClient = 2; //
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
int iRet = 0;
WCHAR GuidString[39] = { 0 };
//unsigned int i, j;
//WCHAR szssid[256];
BOOL bSearchflag = FALSE;
CString strerr, strXml, strHead;
int mxmllenth;
//DWORD dwerrcode;
WCHAR strFile[MAX_BUFF] = { 0 };
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_AVAILABLE_NETWORK pBssEntry = NULL;
int iRSSI = 0;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
strerr.Format(_T("WlanOpenHandle failed with error: %u"), dwResult);
Log(strerr);
return FALSE;
// You can use FormatMessage here to find out why the function failed
}
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
strerr.Format(_T("WlanEnumInterfaces failed with error: %u"), dwResult);
Log(strerr);
return FALSE;
// You can use FormatMessage here to find out why the function failed
}
else {
/*strerr.Format(_T("Num Entries: %lu Current Index: %lu"), pIfList->dwNumberOfItems, pIfList->dwIndex);
Log(strerr);*/
/*strerr.Format(_T("Current Index: %lu"), pIfList->dwIndex);
Log(strerr);*/
pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[0];
dwResult = WlanDisconnect(hClient, &pIfInfo->InterfaceGuid, NULL);//DISCONNECT FIRST
if (dwResult != ERROR_SUCCESS)
{
strerr.Format(_T("WlanDisconnect failed with error: %u"), dwResult);
Log(strerr);
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return FALSE;
}
strHead.Format(_T("<?xml version=\"1.0\"?>\
<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\
<name>%s</name>\
<SSIDConfig>\
<SSID>\
<name>%s</name>\
</SSID>\
</SSIDConfig>\
<connectionType>ESS</connectionType>\
<connectionMode>auto</connectionMode>\
<MSM>\
<security>\
<authEncryption>\
<authentication>open</authentication>\
<encryption>none</encryption>\
<useOneX>false</useOneX>\
</authEncryption>\
</security>\
</MSM>\
</WLANProfile>"), strssid, strssid);
mxmllenth = strHead.GetLength();
if (mxmllenth > MAX_BUFF)
{
Log(_T("the xml contents is over buffer!"));
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return FALSE;
}
for (int j = 0; j < mxmllenth; j++)
{
mbtowc(&strFile[j], (const char *)(LPCTSTR)strHead.Left(1), 1);
strHead = strHead.Right(strHead.GetLength() - 1);
}
strFile[mxmllenth] = '\0';
WLAN_REASON_CODE Wlanreason;
dwResult = WlanSetProfile(hClient, &pIfInfo->InterfaceGuid, 0, strFile, NULL, TRUE, NULL, &Wlanreason);
if (dwResult == ERROR_SUCCESS)
{
Log(_T("WlanSetProfile OK"));
}
else
{
strerr.Format(_T("WlanSetProfile NG,errcode = %d"), GetLastError());
Log(strerr);
}
WLAN_CONNECTION_PARAMETERS wlanConnPara;
wlanConnPara.wlanConnectionMode = wlan_connection_mode_profile;
wlanConnPara.strProfile = strssid;// set the profile name
wlanConnPara.pDot11Ssid = NULL; // SET SSID NULL
wlanConnPara.dot11BssType = dot11_BSS_type_infrastructure;//dot11_BSS_type_any,I do not need it this time.
//If a profile is provided, this BSS type must be the same as the one in the profile.
wlanConnPara.pDesiredBssidList = NULL;
wlanConnPara.dwFlags = WLAN_CONNECTION_HIDDEN_NETWORK;
// set the SSID
/*DOT11_SSID Ssidud = { 0 };
string strSsid;
strSsid = CStringA(strssid);
Ssidud.uSSIDLength = (ULONG)strSsid.size();
memcpy(&Ssidud.ucSSID, strSsid.c_str(), strSsid.size());
wlanConnPara.pDot11Ssid = &Ssidud;*/
//有密码时候连接失败,会返回PASS???
dwResult = WlanConnect(hClient, &pIfInfo->InterfaceGuid, &wlanConnPara, NULL);
if (dwResult != ERROR_SUCCESS)
{
strerr.Format(_T("WlanConnect return code = %u"), dwResult);
Log(strerr);
}
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
WlanCloseHandle(hClient, NULL);
return TRUE;
}
4. 打印无线网络的XML profile属性
DWORD showWlanprofile(WCHAR *pssid)
{
// Declare and initialize variables.
HANDLE hClient = NULL;
DWORD dwMaxClient = 2; //
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
DWORD dwRetVal = 0;
int iRet = 0;
WCHAR GuidString[39] = { 0 };
unsigned int i;
/* variables used for WlanEnumInterfaces */
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
LPCWSTR pProfileName = NULL;
LPWSTR pProfileXml = NULL;
DWORD dwFlags = 0;
DWORD dwGrantedAccess = 0;
// Validate the parameters
pProfileName = pssid;
wprintf(L"Information for profile: %ws\n\n", pProfileName);
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
return 1;
// You can use FormatMessage here to find out why the function failed
}
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
return 1;
// You can use FormatMessage here to find out why the function failed
}
else {
wprintf(L"WLAN_INTERFACE_INFO_LIST for this system\n");
wprintf(L"Num Entries: %lu\n", pIfList->dwNumberOfItems);
wprintf(L"Current Index: %lu\n\n", pIfList->dwIndex);
for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[i];
wprintf(L" Interface Index[%u]:\t %lu\n", i, i);
iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString,
sizeof(GuidString) / sizeof(*GuidString));
// For c rather than C++ source code, the above line needs to be
// iRet = StringFromGUID2(&pIfInfo->InterfaceGuid, (LPOLESTR) &GuidString,
// sizeof(GuidString)/sizeof(*GuidString));
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\n");
dwResult = WlanGetProfile(hClient,
&pIfInfo->InterfaceGuid,
pProfileName,
NULL,
&pProfileXml,
&dwFlags,
&dwGrantedAccess);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanGetProfile failed with error: %u\n",
dwResult);
// You can use FormatMessage to find out why the function failed
}
else {
wprintf(L" Profile Name: %ws\n", pProfileName);
wprintf(L" Profile XML string:\n");
wprintf(L"%ws\n\n", pProfileXml);
wprintf(L" dwFlags:\t 0x%x", dwFlags);
// if (dwFlags & WLAN_PROFILE_GET_PLAINTEXT_KEY)
// wprintf(L" Get Plain Text Key");
if (dwFlags & WLAN_PROFILE_GROUP_POLICY)
wprintf(L" Group Policy");
if (dwFlags & WLAN_PROFILE_USER)
wprintf(L" Per User Profile");
wprintf(L"\n");
wprintf(L" dwGrantedAccess: 0x%x", dwGrantedAccess);
if (dwGrantedAccess & WLAN_READ_ACCESS)
wprintf(L" Read access");
if (dwGrantedAccess & WLAN_EXECUTE_ACCESS)
wprintf(L" Execute access");
if (dwGrantedAccess & WLAN_WRITE_ACCESS)
wprintf(L" Write access");
wprintf(L"\n");
wprintf(L"\n");
}
}
}
if (pProfileXml != NULL) {
WlanFreeMemory(pProfileXml);
pProfileXml = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return dwRetVal;
}