VC++ win7下怎么实现WIFI共享工具
这篇文章主要讲解了“ VC++ win7下怎么实现WIFI共享工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ VC++ win7下怎么实现WIFI共享工具”吧!
关于无线承载网络
无线承载网络(Wireless Hosted Network)是指安装有无线局域网服务的Windows 7和Windows Server 2008 R2所提供的一种WLAN功能,具备两项主要功能:
第一、把一个物理无线网卡虚拟化成一个或多个虚拟无线网卡,称为虚拟的WiFi。
第二、借助无线网卡把当前接网络虚拟成为一个基于软件的无线接入点,称为虚拟AP或软AP。
=========================万恶的分割线===========================
项目需要包含wlanapi.h 头文件和wlanapi.lib 库
备注:测试环境win7_64,建议关闭一些安全软件,(比如我测试的时候忘了关comodo导致防火墙总是拒绝其他设备的连接)。
=============================================================
功能不复杂,用到的主要函数有下面几个
1.调用WlanOpenHandled得到一个连接hClientHandle,下面的操作都要用到这个连接。当然程序退出的时候记得调用WlanCloseHandle关闭这个连接。
DWORD WINAPI WlanOpenHandle( _In_ DWORD dwClientVersion, _Reserved_ PVOID pReserved, _Out_ PDWORD pdwNegotiatedVersion, _Out_ PHANDLE phClientHandle ); DWORD WINAPI WlanCloseHandle( _In_ HANDLE hClientHandle, _Reserved_ PVOID pReserved );
2.下面的函数用来设置承载网络的一些属性。
DWORD WINAPI 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 );typedef enum _WLAN_HOSTED_NETWORK_OPCODE { wlan_hosted_network_opcode_connection_settings, //设置一些连接属性 wlan_hosted_network_opcode_security_settings, //设置安全属性 wlan_hosted_network_opcode_station_profile, //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共享工具这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是蜗牛博客,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
评论