To encode the implementation of the soft AP, you first need to add the corresponding header file and Lib library
The code is as follows |
Copy Code |
#include <wlanapi.h> #include <iphlpapi.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Wlanapi.lib") #pragma comment (lib, "IPHLPAPI.lib") |
Among them, wlanapi.h is the declaration file of the wireless network API, the function name generally begins with the WLAN. Iphlpapi.h is used to invoke the Getadapterinfo function to obtain the network virtual interface information.
Similar to Winsock socket programming, the WLAN library needs to obtain a handle before use, and version negotiation is only implemented in Wlanopenhandle. When the handle is finished, the call Wlanclosehandle closes to release the resource.
The Wlanopenhandle prototype is as follows:
The code is as follows |
Copy Code |
DWORD WINAPI Wlanopenhandle ( _in_ DWORD Dwclientversion, _reserved_ Pvoid Preserved, _out_ Pdword Pdwnegotiatedversion, _out_ Phandle Phclienthandle ); |
dwclientversion Specifies the version of the WLAN that the program expects to use, using a macro wlan_api_version instead. Starting with Vista, the version number for the 2,XP system is 1. The negotiated version is returned through the parameter pdwnegotiatedversion, validated by checking the major version number (shown in the following code). Handle is returned via Phclient.
The code is as follows |
Copy Code |
DWORD dwerror = 0; DWORD dwserviceversion = 0; HANDLE hclient = NULL; if (error_success!= (dwerror = Wlanopenhandle ( Wlan_api_version, NULL,//Reserved &dwserviceversion, &hclient ))) { return-1; } Check service version if (Wlan_api_version_major (dwserviceversion) < Wlan_api_version_major (WLAN_API_VERSION_2_0)) { Wlanclosehandle (Hclient, NULL); } |
Before starting the hosting network, you need to configure the mode to allow state and set the SSID and password. Wlanhostednetworksetproperty can be invoked to set the Wlanhostednetworksetproperty prototype as follows:
code is as follows |
copy code |
dword Wina PI Wlanhostednetworksetproperty ( _in_ HANDLE Hclienthandle, _in_ Wlan_hosted_network_opcode OPCODE, _in_ DWORD dwdatasize, _in_ Pvoid PvData, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ PVOID pvreserved ); |
Where Hclienthandle is the handle we obtained before calling Wlanopenhnadle, dwDataSize specifies the size of the pvdata buffer, and the type of variable that pvdata points to depends on the value of opcode. OpCode is an Wlan_hosted_network_opcode enumeration, and if specified as Wlan_hosted_network_opcode_enable, then pvdata passes in a bool-type variable pointer. Used to indicate whether the hosted network mode is allowed or prohibited (allow/disallow), and if OpCode is wlan_hosted_network_opcode_connection_settings, then pvdata points to Wlan_ Hosted_network_connection_settings structure body that specifies the SSID and maximum number of connections (peer count) that hosts the network.
Sample code:
The code is as follows |
Copy Code |
Set the hosted network mode to allow BOOL bisallow = TRUE; Wlan_hosted_network_reason Dwfailedreason; DWORD Dwreturnvalue = Wlanhostednetworksetproperty (Hwlanclient, Wlan_hosted_network_opcode_enable, sizeof (BOOL), &bisallow, &dwfailedreason, NULL); if (error_success!= dwreturnvalue) { return-1; }
Set the SSID and maximum number of connections for a hosted network Wlan_hosted_network_connection_settings wlanconnectionsetting; memset (&wlanconnectionsetting, 0, sizeof (wlan_hosted_network_connection_settings)); The SSID field in Wlan_hosted_network_connection_settings must be an ANSI type, so if the program is using Unicode, you need to do the conversion. #ifdef _UNICODE WideCharToMultiByte (CP_ACP, 0, Strssid.getbuffer (0),//Save CString type of SSID Strssid.getlength (),//length of the SSID string (LPSTR) WlanConnectionSetting.hostedNetworkSSID.ucSSID, 32, Null NULL); #else memcpy (WlanConnectionSetting.hostedNetworkSSID.ucSSID, Strssid.getbuffer (0), strlen (Strssid.getbuffer (0))); #endif WlanConnectionSetting.hostedNetworkSSID.uSSIDLength = strlen (const char*) WlanConnectionSetting.hostedNetworkSSID.ucSSID); wlanconnectionsetting.dwmaxnumberofpeers = 100; Dwreturnvalue = Wlanhostednetworksetproperty (M_hwlanclient, Wlan_hosted_network_opcode_connection_settings, sizeof (wlan_hosted_network_connection_settings), &wlanconnectionsetting, &dwfailedreason, NULL); if (error_success!= dwreturnvalue) { return-1; }
|
Call the Wlanhostednetworksetsecondarykey function, set the connection password that hosts the network, and the function prototype is as follows:
code is as follows |
copy code |
dword Wina PI Wlanhostednetworksetsecondarykey ( _in_ HANDLE hClientHandle , _in_ DWORD dwkeylength, _in_ Puchar Puckeydata, _in_ BOOL Bispassphrase, _in_ BOOL bpersistent, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ pvoid pvreserved ); |
Hclienthandle is the handle we get before we call Wlanopenhnadle;
Bispassphrase indicates whether the incoming Puckeydata password is in the password format and, if it is a password format, a 8-63-bit ASCII string, set to True, or false if the binary is a 16-binary;
Dwkeylength is the length of the password buffer, which must include the end of the password format.
Puckeydata If the string, you must be an ANSI type, so if the IDE environment configures the string to Unicode, you need to do the conversion, use WideCharToMultiByte, or T2A,W2A macros.
Bpersistent is used to indicate whether the password is persistent. If not, then the password is only valid at this time, otherwise the next time you start soft the AP will still use the password you set this time.
Sample code:
The code is as follows |
Copy Code |
Uchar keybuf[100] = {0}; #ifdef _UNICODE WideCharToMultiByte (CP_ACP, 0, Strsecondarykey.getbuffer (0), Strsecondarykey.getlength (), (LPSTR) Keybuf, 100, Null NULL); #else memcpy (Keybuf, Strsecondarykey.getbuffer (0), strlen (Strsecondarykey.getbuffer (0))); #endif Dwreturnvalue = Wlanhostednetworksetsecondarykey (M_hwlanclient, Strlen ((const char*) keybuf) + 1, Keybuf, TRUE, FALSE, &dwfailedreason, NULL); if (error_success!= dwreturnvalue) { return-1; } |
To start or stop the soft AP, you need to use the following four functions:
The code is as follows |
Copy Code |
DWORD WINAPI wlanhostednetworkstartusing ( _in_ HANDLE Hclienthandle, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ PVOID pvreserved ); DWORD WINAPI wlanhostednetworkstopusing ( _in_ HANDLE Hclienthandle, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ PVOID pvreserved ); DWORD WINAPI Wlanhostednetworkforcestart ( _in_ HANDLE Hclienthandle, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ PVOID pvreserved ); DWORD WINAPI Wlanhostednetworkforcestop ( _in_ HANDLE Hclienthandle, _out_opt_ Pwlan_hosted_network_reason Pfailreason, _reserved_ PVOID pvreserved ); |
The meaning of the parameters is straightforward, and there is no more explanation here. It is worth noting that if a client connects to the AP with a startusing or stopusing version of the function, calling wlanhostednetworkstopusing does not stop the soft AP immediately, but waits until the client actively disconnects Soft AP will not stop. If a version of the function with force is invoked, the soft AP is forced to stop, regardless of whether any clients are connecting to the AP. Of course, calling a function version with force requires that the program be started as an administrator.
Sample code:
The code is as follows |
Copy Code |
dwreturnvalue = Wlanhostednetworkstartusing (M_hwlanclient, &dwfailedreason, NULL); if (Error_success!= Dwreturnvalue) { if (wlan_hosted_network_reason_interface_ unavailable = = Dwfailedreason) { return 0; } return-1; } Dwreturnvalue = wlanhostednetworkstopusing (m_hwlanclient, &dwfailedreason, NULL); if (error_success!= dwreturnvalue) { return-1; } |
After starting the soft AP, binding IP takes a certain amount of time, typically 2-3 seconds. The IP address is generally 192.168.173.1 (or possibly 192.168.137.1).
When you are debugging a program, you can use the soft AP (administrator to open cmd) using the command-line method:
View currently hosted network status-Netsh wlan show hostednetwork
Configure host Network properties, such as SSID, password, and so on-Netsh wlan set hostednetwork ssid=***** key=***** mode=allow|disallow (the asterisk portion is ASCII character, Key must be between 8-63 characters)
Starting the hosting network-netsh wlan start hostednetwork
Turn off the hosting network-netsh wlan stop hostednetwork