本文主要讲解“如何在VC win7下实现WIFI共享工具”。本文的解释简单明了,易学易懂。现在请跟随边肖的思路一起学习学习《如何在VC win7下实现WIFI共享工具》!
关于无线承载网络
无线托管网络是指由安装了无线局域网服务的Windows 7和Windows Server 2008 R2提供的无线局域网功能,主要有两个功能:
首先,将一个物理无线网卡虚拟化为一个或多个虚拟无线网卡,称为虚拟WiFi。
其次,借助无线网卡,将当前连接的网络虚拟化为基于软件的无线接入点,称为虚拟AP或软AP。
=======================万恶的分界线=========================
项目需要包含wlanapi.h头文件和wlanapi.lib库。
注意:测试环境win7_64,建议关闭一些安全软件(比如我在测试的时候忘记关闭comodo,导致防火墙总是拒绝其他设备的连接)。
=============================================================
功能并不复杂,使用的主要功能如下
1.调用WlanOpenHandled获取连接hClientHandle,它将用于以下操作。当然,记得在程序退出时调用WlanCloseHandle来关闭这个连接。
DWORDWINAPIWlanOpenHandle(
_In_DWORDdwClientVersion,
_ Reserved _ PVOIDpReserved,
_ Out _ pdworpdownegotiatedversion,
_Out_PHANDLEphClientHandle
);
DWORDWINAPIWlanCloseHandle(
_In_HANDLEhClientHandle,
_Reserved_PVOIDpReserved
);
2.以下功能用于设置承载网络的一些属性。
DWORDWINAPIWlanHostedNetworkSetProperty(
_In_HANDLEhClientHandle,
_ In _ WLAN _ HOSTED _ NETWORK _ OPcodeOpcode,
_In_DWORDdwDataSize,
_ In _ PVOIDpvData,
_ Out _ opt _ PWLAN _ HOSTED _ NETWORK _ reasonnpfailure,
_ Reserved _ PVOIDpvReserved
);typedef enum _ WLAN _ HOSTED _ NETWORK _ OPCODE {
WLAN _ hosted _ network _ opcode _ connection _ settings//设置一些连接属性
WLAN _ hosted _ network _ opcode _ security _ settings,//设置安全属性
wlan _托管_网络_操作码_站点_配置文件,nb
sp; //query the wireless Hosted Network station profile
wlan_hosted_network_opcode_enable //query or set the wireless Hosted Network enabled flag
} WLAN_HOSTED_NETWORK_OPCODE, *PWLAN_HOSTED_NETWORK_OPCODE;
3.启用和禁用无线承载网络。
打开网络和共享中心 单击更改适配器设置,如果启用了无线承载网络 面板里面就会有描述为Microsoft Virtual WiFi Miniport Adapter的一个虚拟网卡。禁用之后这个虚拟网卡就会消失了。
当然只有启用了无线承载网络才能分享wifi啦
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 );
4.连接和断开网络
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 );
5.用来查询承载网络的网络状况。
DWORD WINAPI WlanHostedNetworkQueryStatus( _In_ HANDLE hClientHandle, _Out_ PWLAN_HOSTED_NETWORK_STATUS *ppWlanHostedNetworkStatus, _Reserved_ PVOID pvReserved );
typedef struct _WLAN_HOSTED_NETWORK_STATUS { WLAN_HOSTED_NETWORK_STATE HostedNetworkState; GUID IPDeviceID; DOT11_MAC_ADDRESS wlanHostedNetworkBSSID; DOT11_PHY_TYPE dot11PhyType; ULONG ulChannelFrequency; DWORD dwNumberOfPeers; //已连接的节点数目 WLAN_HOSTED_NETWORK_PEER_STATE PeerList[1]; //存放节点信息的数组 } WLAN_HOSTED_NETWORK_STATUS, *PWLAN_HOSTED_NETWORK_STATUS; typedef struct _WLAN_HOSTED_NETWORK_PEER_STATE { DOT11_MAC_ADDRESS PeerMacAddress; //节点设备的MAC地址 WLAN_HOSTED_NETWORK_PEER_AUTH_STATE PeerAuthState; //节点的认证状态 } WLAN_HOSTED_NETWORK_PEER_STATE, *PWLAN_HOSTED_NETWORK_PEER_STATE;
调用WlanHostedNetworkQueryStatus将得到一个WLAN_HOSTED_NETWORK_STATUS结构体,就可以查询一些网络基本信息了,比如dwNumberOfPeers连接到这个网络的节点数,PeerList数组存放着对应节点的详细信息,包括MAC地址和认证状态。
6.用来设置密码
注意密码长度必须 大于8 小于63 否则会调用失败
DWORD WINAPI 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 );
======================贴代码==========================
//WIFIMAIN.h 实现文件 #pragma once #include <wlanapi.h> #pragma comment(lib,"wlanapi.lib") // WIFIMAIN class WIFIMAIN : public CWnd { DECLARE_DYNAMIC(WIFIMAIN) public: WIFIMAIN(); virtual ~WIFIMAIN(); protected: DECLARE_MESSAGE_MAP() private: bool allow; bool start; HANDLE hClient; public: int Init(void); int AllowHostedNetWork(void); int DisallowHostedNetWork(void); int StartHostedNetWork(void); int StopHostedNetWork(void); bool isStart(void); bool isAllow(void); int Resume(void); int getpeernumber(void); int SetKEY(CString key); int SetSSID(CString ssidname); };
// WIFIMAIN.cpp : 实现文件 // #include "stdafx.h" #include "wifi共享1028.h" #include "WIFIMAIN.h" // WIFIMAIN IMPLEMENT_DYNAMIC(WIFIMAIN, CWnd) WIFIMAIN::WIFIMAIN() : allow(false) , start(false) { Init(); } WIFIMAIN::~WIFIMAIN() { StopHostedNetWork(); WlanCloseHandle(hClient, NULL); } BEGIN_MESSAGE_MAP(WIFIMAIN, CWnd) END_MESSAGE_MAP() // WIFIMAIN 消息处理程序 int WIFIMAIN::Init(void) { DWORD dwCurVersion = 0; DWORD dwResult = 0; dwResult = WlanOpenHandle(WLAN_API_VERSION, NULL, &dwCurVersion, &hClient); if( ERROR_SUCCESS != dwResult ) { return -1; } BOOL bIsAllow = true; WLAN_HOSTED_NETWORK_REASON dwFailedReason; dwResult = WlanHostedNetworkSetProperty(hClient, wlan_hosted_network_opcode_enable, sizeof(BOOL), &bIsAllow, &dwFailedReason, NULL); if( ERROR_SUCCESS != dwResult ) { return -2; } return 0; } int WIFIMAIN::AllowHostedNetWork(void) { PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL; DWORD dwResult= 0; dwResult=WlanHostedNetworkForceStart(hClient,pFailReason,NULL); if( dwResult != ERROR_SUCCESS ) { return -1; } allow=true; return 0; } int WIFIMAIN::DisallowHostedNetWork(void) { PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL; DWORD dwResult= 0; dwResult=WlanHostedNetworkForceStop(hClient,pFailReason,NULL); if( dwResult != ERROR_SUCCESS ) { return -1; } allow=false; return 0; } int WIFIMAIN::StartHostedNetWork(void) { PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL; DWORD dwResult= 0; dwResult=WlanHostedNetworkStartUsing(hClient,pFailReason,NULL); if( dwResult != ERROR_SUCCESS ) { return -1; } start=true; return 0; } int WIFIMAIN::StopHostedNetWork(void) { PWLAN_HOSTED_NETWORK_REASON pFailReason = NULL; DWORD dwResult= 0; dwResult=WlanHostedNetworkStopUsing(hClient,pFailReason,NULL); if( dwResult != ERROR_SUCCESS ) { return -1; } start=false; return 0; } bool WIFIMAIN::isStart(void) { return start; } bool WIFIMAIN::isAllow(void) { return allow; } int WIFIMAIN::Resume(void) { DWORD dwResult = 0; BOOL bIsAllow = false; WLAN_HOSTED_NETWORK_REASON dwFailedReason; dwResult = WlanHostedNetworkSetProperty(hClient, wlan_hosted_network_opcode_enable, sizeof(BOOL), &bIsAllow, &dwFailedReason, NULL); if( ERROR_SUCCESS != dwResult ) { return -2; } return 0; } int WIFIMAIN::getpeernumber(void) { PWLAN_HOSTED_NETWORK_STATUS ppWlanHostedNetworkStatus=NULL; int retval = WlanHostedNetworkQueryStatus(hClient,&ppWlanHostedNetworkStatus,NULL); if( retval != ERROR_SUCCESS ){ return -1; } return ppWlanHostedNetworkStatus->dwNumberOfPeers; } int WIFIMAIN::SetKEY(CString key) { char chkey[64]; int index; for(index=0; index<key.GetLength(); index++ ) { chkey[index] = key[index]; } chkey[index]='\0'; WLAN_HOSTED_NETWORK_REASON dwFailedReason; DWORD dwResult= 0; dwResult = WlanHostedNetworkSetSecondaryKey(hClient, strlen((const char*)chkey) + 1, (PUCHAR)chkey, TRUE, FALSE, &dwFailedReason, NULL); if(ERROR_SUCCESS != dwResult) { return -1; } return 0; } int WIFIMAIN::SetSSID(CString ssidname) { char chname[64]; int index; for(index=0; index<ssidname.GetLength(); index++ ) { chname[index] = ssidname[index]; } chname[index]='\0'; DWORD dwResult= 0; WLAN_HOSTED_NETWORK_REASON dwFailedReason; WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS cfg; memset(&cfg, 0, sizeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS)); memcpy( cfg.hostedNetworkSSID.ucSSID, chname, strlen(chname) ) ; cfg.hostedNetworkSSID.uSSIDLength = strlen((const char*)cfg.hostedNetworkSSID.ucSSID); cfg.dwMaxNumberOfPeers = 20; //最大连接数 dwResult = WlanHostedNetworkSetProperty(hClient, wlan_hosted_network_opcode_connection_settings, sizeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS), &cfg, &dwFailedReason, NULL); if(ERROR_SUCCESS != dwResult ) { return -1; } return 0; }
感谢各位的阅读,以上就是“ VC++ win7下怎么实现WIFI共享工具”的内容了,经过本文的学习后,相信大家对 VC++ win7下怎么实现WIFI共享工具这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/127472.html