【轉】Windows 7 API Internet Connection Sharing(ICS) 與 Wireless Hosted Network構建本地AP

原文:http://hi.baidu.com/ritrachiao/item/bf7715e6bb8cb3a0c10d75behtml

【此刻我要大大地記錄一下!】windows

這個折騰了我好幾天的Windows 7 API終於讓我搞定了!api

利用Windows ICS API 和Windows 7 API中全新的Wireless Hosted Network,將本地的有線鏈接共享給Microsoft虛擬Wifi,使得本地計算機在windows 7下變成AP!網絡

這個操做的手動操做部分,我在前一篇文章[譯] 關於無線承載網絡(Wireless Hosted Network)中已經講明白了,可是Windows 7爲咱們提供了相關操做的API,因此,我想,能否不須要去記錄那些繁瑣的命令,也不要打開命令行,只須要用一個C++程序,將全部的操做封裝在一塊兒,那麼即可以實現!app

因而,開啓了我苦逼地閱讀Windows API的旅程。。less

其實原理很是簡單,首先想辦法開啓Windows的ICS服務,而後開啓Wireless Hosted Network,再將兩個關聯起來。但是,就是這麼easy的邏輯,老紙找這些API找了很久啊~~~主要也是以前不懂這些API怎麼用,再加上一直查不到相關的資料,最後,幾番折騰,終於在官方文檔、走過無數彎路、MSDN的友情幫助,以及各大論壇的細枝末節中,我終於找到蛛絲馬跡,將功能徹底實現了!函數

廢話很少說,直接上代碼!ui

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++this

 

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

 

#define WIN32_LEAN_AND_MEAN

 

#include "stdafx.h"

 

#include <windows.h>

#include <objbase.h>

#include <netcon.h>

#include <stdio.h>

 

#include <NetCon.h>

 

#include <locale.h>

 

#include <wlanapi.h>

#include <WinNetWk.h>

#include <netlistmgr.h>

 

#include <ShObjIdl.h>

 

#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,思來想去找了不少種辦法,都不成功,最後幾經周折,功夫不負有心人,終於將問題搞定!

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

相關文章
相關標籤/搜索