Windows 7 API Internet Connection Sharing(ICS) 与 Wireless Hosted Network构建

2016-12-02 12:52:03来源:网络收集作者:Max人点击


【此刻我要大大地记录一下!】



这个折腾了我好几天的Windows 7 API终于让我搞定了!



利用Windows ICS API 和Windows 7 API中全新的Wireless Hosted Network,将本地的有线连接共享给Microsoft虚拟Wifi,使得本地计算机在windows 7下变成AP!



这个操作的手动操作部分,我在前一篇文章[译] 关于无线承载网络(Wireless Hosted Network)中已经讲明白了,但是Windows 7为我们提供了相关操作的API,所以,我想,可否不需要去记录那些繁琐的命令,也不要打开命令行,只需要用一个C++程序,将所有的操作封装在一起,那么便可以实现!



于是,开启了我苦逼地阅读Windows API的旅程。。



其实原理非常简单,首先想办法开启Windows的ICS服务,然后开启Wireless Hosted Network,再将两个关联起来。可是,就是这么easy的逻辑,老纸找这些API找了好久啊~~~主要也是之前不懂这些API怎么用,再加上一直查不到相关的资料,最后,几番折腾,终于在官方文档、走过无数弯路、MSDN的友情帮助,以及各大论坛的细枝末节中,我终于找到蛛丝马迹,将功能完全实现了!



废话不多说,直接上代码!





++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++





// Copyright (C) Microsoft. All rights reserved.





#define WIN32_LEAN_AND_MEAN





#include "stdafx.h"





#include



#include



#include



#include





#include





#include





#include



#include



#include





#include





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



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





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



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





// as in winsock.h



#define NAT_PROTOCOL_TCP 6



#define NAT_PROTOCOL_UDP 17





HRESULT AddSharingNet (INetSharingManager* pNSM)



{



INetConnection * pNC = NULL; // fill this out for part 2 below





INetSharingEveryConnectionCollection * pNSECC = NULL;



HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);



if (!pNSECC)



wprintf (L"failed to get EveryConnectionCollection!/r/n");



else {



// enumerate connections



IEnumVARIANT * pEV = NULL;



IUnknown * pUnk = NULL;



hr = pNSECC->get__NewEnum (&pUnk);



if (pUnk) {



hr = pUnk->QueryInterface (__uuidof(IEnumVARIANT),



(void**)&pEV);



pUnk->Release();



}



if (pEV) {



VARIANT v;



VariantInit (&v);



BOOL bFoundIt = FALSE;



INetSharingConfiguration * pNSC = NULL;



INetSharingConfiguration * pVWifiNSC = NULL;



NETCON_PROPERTIES* pVWifi = NULL;





while (S_OK == pEV->Next (1, &v, NULL)) {



if (V_VT (&v) == VT_UNKNOWN) {



V_UNKNOWN (&v)->QueryInterface (__uuidof(INetConnection),



(void**)&pNC);



if (pNC) {



NETCON_PROPERTIES* pNP = NULL;



pNC->GetProperties(&pNP);



setlocale(LC_ALL, "chs");





wprintf(L"pszwName--%s/n",pNP->pszwName);



wprintf(L"pszwDeviceName--%s/n",pNP->pszwDeviceName);



wprintf(L"Status--%d/n",pNP->Status);



wprintf(L"MediaType--%d/n",pNP->MediaType);



wprintf(L"dwCharacter--%d/n",pNP->dwCharacter);



wprintf(L"clsidThisObject--%d/n",pNP->clsidThisObject);



wprintf(L"clsidUiObject--%d/n",pNP->clsidUiObject);





wprintf(L"/n");





if(pNP->Status == NCS_CONNECTED){



hr = pNSM->get_INetSharingConfigurationForINetConnection (pNC, &pNSC);



hr = pNSC->EnableSharing(ICSSHARINGTYPE_PUBLIC);



pNSC->Release();





}





if(!strcmp((char*)(pNP->pszwName),(char*)(L"leonchiao"))){



pVWifi = pNP;



hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC,&pVWifiNSC);



pVWifiNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE);



wprintf(L"/nIs this VWifi?--%s/n/n",pVWifi->pszwDeviceName);



}



}



}



}





INetSharingPortMapping * pNSPM = NULL;



BSTR pszwName = pVWifi->pszwName;



wprintf(L"BSTR--------------%s/n",pszwName);





pVWifiNSC->Release();





if (pNSPM) {



wprintf (L"just added ........................................................!/r/n");





hr = pNSPM->Enable();



wprintf (L"just enabled port mapping!/r/n");





INetSharingPortMappingProps* pMapping = NULL;



pNSPM->get_Properties(&pMapping);



BSTR result = NULL;



long port = 0;



pMapping->get_Name(&result);



pMapping->get_ExternalPort(&port);



wprintf(L"NAMEEEEE::::%s/n",result);



wprintf(L"exPortttttt::::%d/n",port);





pNSPM->Release();



}





}



}









return hr;



}





HRESULT StartHostednetwork()



{



DWORD dwNegotiatedVersion = 1;



HANDLE hClientHandle = 0;



DWORD dwClientVersion = 2;





DWORD hWlan;



DWORD hStart;



WLAN_HOSTED_NETWORK_REASON failReason;



char result[600];





DWORD dwKeyLength = 10;



BOOL bIsPassPhrase = true;



BOOL bPersistent = true;



DWORD hSecondkey;





hWlan = WlanOpenHandle(dwClientVersion,NULL,&dwNegotiatedVersion,&hClientHandle);





DWORD dwRetVal;



TCHAR UserName[80];



DWORD dwNameLength = sizeof(UserName);





if(hWlan == ERROR_SUCCESS)



{



hStart = WlanHostedNetworkStartUsing (hClientHandle,&failReason,NULL);



wprintf(L"Starting Hostednetwork!/n");



}





return hStart;





}





int main()



{



CoInitialize (NULL);





// init security to enum RAS connections



CoInitializeSecurity (NULL, -1, NULL, NULL,



RPC_C_AUTHN_LEVEL_PKT,



RPC_C_IMP_LEVEL_IMPERSONATE,



NULL, EOAC_NONE, NULL);





INetSharingManager * pNSM = NULL;



HRESULT hr = ::CoCreateInstance (__uuidof(NetSharingManager),



NULL,



CLSCTX_ALL,



__uuidof(INetSharingManager),



(void**)&pNSM);



if (!pNSM)



wprintf (L"failed to create NetSharingManager object/r/n");



else{



AddSharingNet(pNSM);



StartHostednetwork();



}





getchar();





return (int)hr;



}





++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



我简单解释一下代码:(1)第一个函数是HRESULT AddSharingNet (INetSharingManager* pNSM),这个函数的作用就是开启ICS服务,并且将ICS服务共享出去。除此之外,我在函数中将所有的网络适配器都打印了出来,可以为进一步开发做铺垫。这里面比较重要的一个地方是用红色标记出来的,也就是说,ICS需要PUBLIC的共享。另一个重要的地方是用蓝色标记出来的,这里主要说明的是,在找到Microsoft 虚拟Wifi之后,你需要为这个虚拟wifi做映射,那么映射之前你得先找到这个网络适配器,所以就先将它保存在pVWifiNSC中,然后这个时候记录下来的适配器是虚拟Wifi,这是将虚拟wifi进行PRIVATE共享,它会自动和本机的有线连接进行关联,实现内部的ICS共享。这一步就是这么简单,但是就是这么简单,然我尝试了N种方法,最终终于搞定!



(2)第二个函数是HRESULT StartHostednetwork(),这个函数很简单,就是开启Wireless Hosted Network,如果你对这里不熟悉,很容易在Windows
7 API W部分找到这些API,而且我的上一篇文章页提到了如何使用这些API。





就这样,整个工作就结束了,老纸可是花了九牛二虎之力才搞定的,真的不容易啊!



期间走了很多弯路,而且发现没有其他人写过类似的代码,连个可以参考的都没有,最后自己摸黑走,实验了很多种API,思来想去找了很多种办法,都不成功,最后几经周折,功夫不负有心人,终于将问题搞定!



不得不感叹一下啊!哈哈~




上文来自:http://www.cnblogs.com/SummerRain/archive/2013/08/20/3269269.html

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台