无线网编程之连接无线网(一)

在写连接无线网之前,顺便写了一下断开连接的函数(比连接简单了很多很多-_-|||)

DWORD WINAPI WlanDisconnect(
  _In_        HANDLE hClientHandle,
  _In_        const GUID *pInterfaceGuid,
  _Reserved_  PVOID pReserved
);

函数很简单,就三个参数,作用就是断开网卡pInterfaceGuid上的网络连接。

	//////////////////////断开网络//////////////////
	dwResult= WlanDisconnect(
											  hClient,
											  &pIfInfo->InterfaceGuid,
											  NULL
											);
	if(dwResult==ERROR_SUCCESS)
	{
		wprintf(L"Disconnect successfully!n");
	}
	else
	{
		wprintf(L"Error code:%d",dwResult);
		wprintf(L"参考值:ERROR_INVALID_PARAMETER:%lun",ERROR_INVALID_PARAMETER);
		wprintf(L"             ERROR_INVALID_HANDLE:%lun",ERROR_INVALID_HANDLE);
		//wprintf(L"             RPC_STATUS:%lun",RPC_STATUS);//RPC_STATUS不允许使用类型名?
		wprintf(L"             ERROR_NOT_ENOUGH_MEMORY:%lun",ERROR_NOT_ENOUGH_MEMORY);
		wprintf(L"             ERROR_ACCESS_DENIED:%lun",ERROR_ACCESS_DENIED);
		return 1;
	}

 

然后来看连接函数

WlanConnect

DWORD WINAPI WlanConnect(
  _In_        HANDLE hClientHandle,
  _In_        const GUID *pInterfaceGuid,
  _In_        const PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
  _Reserved_  PVOID pReserved
);

看起来很简单,只有4个参数,而且其中三个都很熟悉,关键是第三个参数pConnectionParameters ,来看下这个参数的定义

typedef struct _WLAN_CONNECTION_PARAMETERS {
  WLAN_CONNECTION_MODE wlanConnectionMode;
  LPCWSTR              strProfile;
  PDOT11_SSID          pDot11Ssid;
  PDOT11_BSSID_LIST    pDesiredBssidList;
  DOT11_BSS_TYPE       dot11BssType;
  DWORD                dwFlags;
} WLAN_CONNECTION_PARAMETERS, *PWLAN_CONNECTION_PARAMETERS;

wlanConnectionMode:WLAN_CONNECTION_MODE 枚举值,其值为

typedef enum _WLAN_CONNECTION_MODE { 
  wlan_connection_mode_profile,
  wlan_connection_mode_temporary_profile,
  wlan_connection_mode_discovery_secure,
  wlan_connection_mode_discovery_unsecure,
  wlan_connection_mode_auto,
  wlan_connection_mode_invalid
} WLAN_CONNECTION_MODE, *PWLAN_CONNECTION_MODE;

直接按照字面意思理解,一般用第一个第二个比较多吧,wlan_connection_mode_profile 表示用用已有的profile来连接无线网,wlan_connection_mode_temporary_profile 表示用一个临时的profile来连接无线网。其他几个值还有待实验。

strProfile:这个参数之前接触过,LPCWSTR字符串类型,就是ProfileInfo的strProfileName值。如果wlanConnectionMode 被设置为 wlan_connection_mode_profile, 那么需要指定strProfile 为Profile的名称。如果 wlanConnectionMode 被设置为wlan_connection_mode_temporary_profile, 那么 strProfile 要指定一个XML格式的配置信息,也就是前面GetProfile函数里面的strProfileXml. 如果 wlanConnectionMode 设置为 wlan_connection_mode_discovery_secure 或者 wlan_connection_mode_discovery_unsecure, 那么strProfile 设置为NULL。

pDot11Ssid:指定无线网的SSID,当然是DOT11_SSID 结构体类型。这个参数是可选的,当设置为NULL的时候,Profile里面的所有SSID都会尝试连接。但当WLAN_CONNECTION_MODE设置为wlan_connection_mode_discovery_secure 或者wlan_connection_mode_discovery_unsecure时,这个参数不能为NULL。

pDesiredBssidList:BSSID列表,可以通过WlanGetNetworkBssList函数获取。

dot11BssType:BSS的类型,如果Profile提供了该值,则此参数应该和Profile里的保持一致。反正Profile XML没有看到这个值,但是根据WlanGetNetworkBssList获取到的BSS列表来看,在我的电脑上都是dot11_BSS_type_infrastructure这个值,所以我就用这个值了。

dwFlags:标识位,用来指定连接的属性的。有以下值

Constant Value Description
WLAN_CONNECTION_HIDDEN_NETWORK 0x00000001 Connect to the destination network even if the destination is a hidden network. A hidden network does not broadcast its SSID. Do not use this flag if the destination network is an ad-hoc network.If the profile specified by strProfile is notNULL, then this flag is ignored and the nonBroadcast profile element determines whether to connect to a hidden network.
WLAN_CONNECTION_ADHOC_JOIN_ONLY 0x00000002 Do not form an ad-hoc network. Only join an ad-hoc network if the network already exists. Do not use this flag if the destination network is an infrastructure network.
WLAN_CONNECTION_IGNORE_PRIVACY_BIT 0x00000004 Ignore the privacy bit when connecting to the network. Ignoring the privacy bit has the effect of ignoring whether packets are encrypted and ignoring the method of encryption used. Only use this flag when connecting to an infrastructure network using a temporary profile.
WLAN_CONNECTION_EAPOL_PASSTHROUGH 0x00000008 Exempt EAPOL traffic from encryption and decryption. This flag is used when an application must send EAPOL traffic over an infrastructure network that uses Open authentication and WEP encryption. This flag must not be used to connect to networks that require 802.1X authentication. This flag is only valid when wlanConnectionMode is set towlan_connection_mode_temporary_profile. Avoid using this flag whenever possible.

这个参数可以叠加使用,把对应的值相加即可。这个值的设置应该影响不大。我直接用第一个值了~

代码~~~

	////////////////连接无线网测试////////////////////
	WLAN_CONNECTION_PARAMETERS ConnectionParameters={//必须const?
					wlan_connection_mode_profile,
					pWPIList->ProfileInfo[0].strProfileName,
					NULL,		//NULL的话就用profile里的ssid
					NULL,		//这个参数可能需要由WlanGetNetworkBssList 获取的dot11Bssid组成列表
					dot11_BSS_type_infrastructure ,		//不知道这个值不同会不会有影响
					WLAN_CONNECTION_HIDDEN_NETWORK,		//strProfile设置了,这个参数被忽略

																		};
	dwResult = WlanConnect(
				hClient,
				&pIfInfo->InterfaceGuid,
				&ConnectionParameters,
				NULL
										);
	if (dwResult != ERROR_SUCCESS)  {
        wprintf(L"WlanConnect failed with error: %un", dwResult);
        // FormatMessage can be used to find out why the function failed
        return 1;
    }
    else {
		wprintf(L"Connect successfully!nn");
	}

但是这个函数有个问题就是不会返回连接结果。如果配置不出问题,成功地发出连接请求,都会输出“Connect successfully!”,但是能不能连接上就不知道了,比如无线网关了、密码改了……

忘记说WlanConnect函数的返回值了,成功ERROR_SUCCESS,其它有以下值

Return code Description
ERROR_INVALID_PARAMETER
One of the following conditions occurred: 

  • hClientHandle is NULL or invalid.
  • pInterfaceGuid is NULL.
  • pConnectionParameters is NULL.
  • The dwFlags member of the structure pointed to bypConnectionParameters is not set to one of the values specified on theWLAN_CONNECTION_PARAMETERS page.
  • The wlanConnectionMode member of the structure pointed to bypConnectionParameters is set towlan_connection_mode_discovery_secure orwlan_connection_mode_discovery_unsecure, and the pDot11Ssidmember of the same structure is NULL.
  • The wlanConnectionMode member of the structure pointed to bypConnectionParameters is set towlan_connection_mode_discovery_secure orwlan_connection_mode_discovery_unsecure, and the dot11BssTypemember of the same structure is set to dot11_BSS_type_any.
  • The wlanConnectionMode member of the structure pointed to bypConnectionParameters is set to wlan_connection_mode_profile, and the strProfile member of the same structure is NULL or the length of the profile exceeds WLAN_MAX_NAME_LENGTH.
  • The wlanConnectionMode member of the structure pointed to bypConnectionParameters is set to wlan_connection_mode_profile, and the strProfile member of the same structure is NULL or the length of the profile is zero.
  • The wlanConnectionMode member of the structure pointed to bypConnectionParameters is set to wlan_connection_mode_invalid orwlan_connection_mode_auto.
  • The dot11BssType member of the structure pointed to bypConnectionParameters is set to dot11_BSS_type_infrastructure, and the dwFlags member of the same structure is set toWLAN_CONNECTION_ADHOC_JOIN_ONLY.
  • The dot11BssType member of the structure pointed to bypConnectionParameters is set to dot11_BSS_type_independent, and the dwFlags member of the same structure is set toWLAN_CONNECTION_HIDDEN_NETWORK.
  • The dwFlags member of the structure pointed to bypConnectionParameters is set toWLAN_CONNECTION_IGNORE_PRIVACY_BIT, and either thewlanConnectionMode member of the same structure is not set towlan_connection_mode_temporary_profile or the dot11BssTypemember of the same structure is set todot11_BSS_type_independent.
ERROR_INVALID_HANDLE
The handle hClientHandle was not found in the handle table.
RPC_STATUS
Various error codes.
ERROR_ACCESS_DENIED
The caller does not have sufficient permissions. 

注意:这个函数执行会立即返回,但是返回的是以上错误码,不会告诉你连接的结果:密码错误等等,要想获取连接信息,需要了解WlanRegisterNotification函数,明天继续研究。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据