我没有使用WlanConnect()和WlanConnect()返回ERROR_SUCCESS与不安全的网络连接 [英] I am not getting connect with unsecure network with WlanConnect () and WlanConnect() return ERROR_SUCCESS

查看:388
本文介绍了我没有使用WlanConnect()和WlanConnect()返回ERROR_SUCCESS与不安全的网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#ifndef UNICODE
#define UNICODE
#endif

#pragma comment(lib, "wlanapi.lib")

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <string.h>

#include <wlanapi.h>

int wmain()
{
	DWORD dwResult;
	DWORD con;
	bool status = 0;
	HANDLE hClient;
	DWORD dwRealAPIVer;

	PWLAN_INTERFACE_INFO_LIST pInterfaces;
	PWLAN_INTERFACE_INFO pInterface;

	PWLAN_AVAILABLE_NETWORK_LIST pNetworks;
	PWLAN_AVAILABLE_NETWORK pNetwork;

	// 1) Connecting Wi-Fi
	dwResult = WlanOpenHandle(2, NULL, &dwRealAPIVer, &hClient);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't connect Wi-Fi! Problem function: WlanOpenHandle.");
		_getch();
		return 1;
	}

	// 2) Enumerating Wi-Fi adapters (interfaces)
	dwResult = WlanEnumInterfaces(hClient, NULL, &pInterfaces);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't enumerate WLAN adapters! Problem function: WlanEnumInterfaces.");
		_getch();
		return 2;
	}
	if (pInterfaces->dwNumberOfItems == 0)
	{
		wprintf(L"No Wi-Fi adapter found.");
		_getch();
		return 3;
	}

	// 3) Getting current Wi-Fi adapter.
	pInterface = &pInterfaces->InterfaceInfo[pInterfaces->dwIndex];

	// 4) Getting available networks for this adapter.
	dwResult = WlanGetAvailableNetworkList(hClient, &pInterface->InterfaceGuid, 0, NULL, &pNetworks);
	if (FAILED(dwResult))
	{
		wprintf(L"Can't get available networks! Probably your PC in Airplane mode, and/or Wifi turned out. Problem function: WlanGetAvailableNetworkList.");
		_getch();
		return 4;
	}

	// 5) Enumertating networks
	for (int i = 0; i < (int)pNetworks->dwNumberOfItems; i++)
	{
		// 6) Getting current network from list
		pNetwork = &pNetworks->Network[i];

		// 7) Getting SSID (network's name)
		DOT11_SSID oName = pNetwork->dot11Ssid;
		DOT11_AUTH_ALGORITHM auth = pNetwork->dot11DefaultAuthAlgorithm;
		if (oName.uSSIDLength == 0)
		{
			wprintf(L"(Unknown network name)");
		}
		else
		{
			// 8) Output all characters of SSID
			for (int j = 0; j < (int)oName.uSSIDLength; j++)
			{
				wprintf(L"%c", oName.ucSSID[j]);
			}
		}
		if (auth == 1)
		{
			// parameter for wlanconnect
			WLAN_CONNECTION_PARAMETERS connectionParams;
			connectionParams.wlanConnectionMode = wlan_connection_mode_discovery_unsecure;
			connectionParams.strProfile = NULL;
			char *nameOfSSID = "Window";
			int lengthOfSSID = strlen((const char*)nameOfSSID);
			DOT11_SSID infoForSSID;
			strncpy((char *)(infoForSSID.ucSSID), nameOfSSID, lengthOfSSID);
			infoForSSID.uSSIDLength = lengthOfSSID;
			connectionParams.pDot11Ssid = &infoForSSID;
			connectionParams.pDesiredBssidList = NULL;
			connectionParams.dot11BssType = dot11_BSS_type_independent;
			connectionParams.dwFlags = WLAN_CONNECTION_ADHOC_JOIN_ONLY;

			wprintf(L"\tOpen ");
			// Now connected with open networks
			con = WlanConnect(hClient, &pInterface->InterfaceGuid, &connectionParams, NULL);
			if (FAILED(con))
			{
				wprintf(L"connection not working");
				_getch();
				return 5;
			}
			else
			{
				wprintf(L"connected succesfully");
			}
		}
		else
		{
			wprintf(L"\tSecure");
		}
		wprintf(L"\n");
	}

	_getch();

	// 9) Removing unnecessary lists from RAM
	if (pInterfaces != NULL)
	{
		WlanFreeMemory(pInterfaces);
	}
	if (pNetworks != NULL)
	{
		WlanFreeMemory(pNetworks);
	}

	return 0;
}

推荐答案

虽然我们正在尽力让读者发布问题,但我们仍然处于头脑风暴阶段... 所以请更具体一点!

如果您的问题是当WlanConnet()返回连接尚未完成时请更好地阅读关于WlanConnect() [ ^ ],你会发现那个返回时连接未完成,但正在完成中。要在连接完成时收到通知,您必须使用 WlanRegisterNotification() [ ^ ]。

ERROR_SUCCESS值意味着WlanConnect()正确地完成了所有操作,并且还正确启动了连接过程。连接的结果可通过WlanRegisterNotification()获得。
While we are doing our best to mind-read who post questions, we still are on the brainstorm phase... So please be more specific!
If your problem is that when WlanConnet() returns the connection is not completed yet please read better about WlanConnect()[^], and you'll discover that the connection is not completed when it returns, but is on the way for completion. To be notified when connection is completed you have to register for a callback notification using WlanRegisterNotification()[^].
The ERROR_SUCCESS value means simply that the WlanConnect() made everything correctly and also correctly started the connection procedure. The outcome of the connection is available through WlanRegisterNotification().


我的问题是

1.扫描所有wifi。

2.并显示只打开wifi。

3.连接wifi并在5秒内断开连接



再次连接另一个wifi并断开连接。



做类似连接所有Open wifi
My problem is that
1. Scan the all wifi.
2. And show Only open wifi.
3. Connect a wifi and disconnected in 5 second

again connected with another wifi and disconnected.

Do Similar connected with all Open wifi


这篇关于我没有使用WlanConnect()和WlanConnect()返回ERROR_SUCCESS与不安全的网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆