2282|0

6802

帖子

0

TA的资源

五彩晶圆(高级)

楼主
 

如何启用Win7自带的虚拟无线AP功能供Android等无线设备上网 [复制链接]

无线承载网络(Wireless Hosted Network)是Windows 7和安装有 WLAN 服务的Windows Server 2008 R2中一项新增的WLAN特性。该特性实现了以下两个主要功能:         1.将物理无线网卡虚拟为一个以上的虚拟无线网卡,即虚拟"WIFI"。         2.通过特定的虚拟无线网卡实现基于软件的无线AP(Access Point, "接入点" 或 "访问点"),即"软AP(SoftAP)"。
这两个功能同时存在于 Windows 系统中。启用和禁用无线承载网络时也就同时启用和禁用了“虚拟WiFi”和“软AP”。无法在Windows中分别禁用或启用这两个功能。

通过此特性,Windows 计算机能通过一块物理无线网卡以客户端身份连接到(由物理设备提供的)硬AP,同时又能作为软AP,允许其它设备与自己连接。此特性需要计算机上安装有支持“承载网络”功能的无线网卡。无线网卡的驱动程序必须实现微软为 Windows 7 定义的“无线LAN设备驱动程序模型”。 无线网卡驱动程序必须支持“无线承载网络”特性,才能取得“Windows 7 徽标认证”。

本文通过三种方式启用Win7下的承载网络:

第一种方案

命令方式:

要求:

                本命令的执行需要管理员权限,如果你的计算机用户名不是Administrator,你需右击以管理员身份运行命令行。

开启:

                1.netsh wlan set hostednetwork mode=allow        //设置承载网络为允许

                2.netsh wlan set hostednetwork ssid="热点的名字,自己命名" key="自己设置密码,大于等于8位即可"        //设置热点名和密码

                3.netsh wlan start hostednetwork        //启用承载网络

停止:

                1.netsh wlan stop hostednetwork        //停止承载网络

说明:

                1.更改密码

                netsh wlan set hostednetwork key="你的密码"

                如果想密码立即生效可以用

                netsh wlan refresh hostednetwork key

                2.更改网络名称

                netsh wlan set hostednetwork ssid="你的名称"

                最后就是你真的不用这个无线网络了,你停止之后,也不想看到多出来的那块无线网卡,那么就执行

                netsh wlan set hostednetwork mode=

                就再也看不到那块网卡了,当然你想重新开启的话就执行

                netsh wlan set hostednetwork mode=allow

批处理完成:

                开启:

                netsh wlan set hostednetwork mode=allow

                netsh wlan set hostednetwork ssid="热点的名字,自己命名" key="自己设置密码,大于等于8位即可"

                netsh wlan start hostednetwork

                复制上面三行到记事本,命名为Start.bat或Start.cmd,注意扩展名,右击以管理员身份运行即可。

                停止:

                netsh wlan stop hostednetwork

                复制上面一行到记事本,命名为Stop.bat或Stop.cmd,注意扩展名,右击以管理员身份运行即可。

最后 :

       1. 打开网络共享中心->更改设配器设置

       2.右击本地连接->属性->共享

       3.勾选允许其他用户通过此计算机的Internet连接来连接

       4.在下面框中选择刚启动的虚拟无线网卡



程序员篇:

第二种方案

通过C#实现,本处需要用代码启用共享,无需手动进行设置,所以需要用到一个vbs的脚本

脚本内容如下:

        OPTION EXPLICIT
DIM ICSSC_DEFAULT, CONNECTION_PUBLIC, CONNECTION_PRIVATE, CONNECTION_ALL
DIM NetSharingManager
DIM PublicConnection, PrivateConnection
DIM EveryConnectionCollection

DIM objArgs
DIM priv_con, publ_con
dim switch

ICSSC_DEFAULT         = 0
CONNECTION_PUBLIC     = 0
CONNECTION_PRIVATE    = 1
CONNECTION_ALL        = 2

Main()

sub Main( )
    Set objArgs = WScript.Arguments

    if objArgs.Count = 3 then
        priv_con = objArgs(0)
  publ_con = objArgs(1)
  switch = objArgs(2)

        if Initialize() = TRUE then
            GetConnectionObjects()
            FirewallTestByName priv_con,publ_con
        end if
    else
        DIM szMsg
        if Initialize() = TRUE then
            GetConnectionObjects()
            FirewallTestByName "list","list"
        end if

        szMsg = "To share your internet connection, please provide the name of the private and public connections as the argument." & vbCRLF & vbCRLF & _
                "Usage:" & vbCRLF & _
                "       " & WScript.scriptname & " " & chr(34) & "Private Connection Name" & chr(34) & " " & chr(34) & "Public Connection Name" & chr(34)
        WScript.Echo( szMsg & vbCRLF & vbCRLF)
    end if
end sub

sub FirewallTestByName(con1,con2)
on error resume next
    DIM Item
    DIM EveryConnection
    DIM objNCProps
    DIM szMsg
    DIM bFound1,bFound2

    WScript.echo(vbCRLF & vbCRLF)
    bFound1 = false
    bFound2 = false
    for each Item in EveryConnectionCollection
        set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
        set objNCProps = NetSharingManager.NetConnectionProps(Item)
        szMsg = "Name: "       & objNCProps.Name & vbCRLF & _
                "Guid: "       & objNCProps.Guid & vbCRLF & _
                "DeviceName: " & objNCProps.DeviceName & vbCRLF & _
                "Status: "     & objNCProps.Status & vbCRLF & _
                "MediaType: "  & objNCProps.MediaType
        if EveryConnection.SharingEnabled then
            szMsg = szMsg & vbCRLF & _
                    "SharingEnabled" & vbCRLF & _
                    "SharingType: " & ConvertConnectionTypeToString(EveryConnection.SharingConnectionType)
        end if

        if objNCProps.Name = con1 then
            bFound1 = true
            if EveryConnection.SharingEnabled = False and switch="on" then
                szMsg = szMsg & vbCRLF & "Not Shared... Enabling private connection share..."
                WScript.Echo(szMsg)
                EveryConnection.EnableSharing CONNECTION_PRIVATE
                szMsg = " Shared!"
      elseif(switch = "off") then
        szMsg = szMsg & vbCRLF & "Shared... DisEnabling private connection share..."
                WScript.Echo(szMsg)
        EveryConnection.EnableSharing CONNECTION_ALL

            end if
          end if

        if objNCProps.Name = con2 then
            bFound2 = true
            if EveryConnection.SharingEnabled = False and switch="on" then
                szMsg = szMsg & vbCRLF & "Not Shared... Enabling public connection share..."
                WScript.Echo(szMsg)
                EveryConnection.EnableSharing CONNECTION_PUBLIC
                szMsg = " Shared!"
      elseif(switch = "off") then
        szMsg = szMsg & vbCRLF & "Shared... DisEnabling public connection share..."
                WScript.Echo(szMsg)
        EveryConnection.EnableSharing CONNECTION_ALL
            end if
        end if
        WScript.Echo(szMsg & vbCRLF & vbCRLF)
    next

    if( con1 <> "list" ) then
        if( bFound1 = false ) then
            WScript.Echo( "Connection " & chr(34) & con1 & chr(34) & " was not found" )
        end if
        if( bFound2 = false ) then
            WScript.Echo( "Connection " & chr(34) & con2 & chr(34) & " was not found" )
        end if
    end if
end sub

function Initialize()
    DIM bReturn
    bReturn = FALSE

    set NetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1")
    if (IsObject(NetSharingManager)) = FALSE then
        Wscript.Echo("Unable to get the HNetCfg.HnetShare.1 object")
    else
        if (IsNull(NetSharingManager.SharingInstalled) = TRUE) then
            Wscript.Echo("Sharing isn't available on this platform.")
        else
            bReturn = TRUE
        end if
    end if
    Initialize = bReturn
end function

function GetConnectionObjects()
    DIM bReturn
    DIM Item

    bReturn = TRUE

    if GetConnection(CONNECTION_PUBLIC) = FALSE then
        bReturn = FALSE
    end if

    if GetConnection(CONNECTION_PRIVATE) = FALSE then
        bReturn = FALSE
    end if

    if GetConnection(CONNECTION_ALL) = FALSE then
        bReturn = FALSE
    end if

    GetConnectionObjects = bReturn

end function


function GetConnection(CONNECTION_TYPE)
    DIM bReturn
    DIM Connection
    DIM Item
    bReturn = TRUE

    if (CONNECTION_PUBLIC = CONNECTION_TYPE) then
        set Connection = NetSharingManager.EnumPublicConnections(ICSSC_DEFAULT)
        if (Connection.Count > 0) and (Connection.Count < 2) then
            for each Item in Connection
                set PublicConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
            next
        else
            bReturn = FALSE
        end if
    elseif (CONNECTION_PRIVATE = CONNECTION_TYPE) then
        set Connection = NetSharingManager.EnumPrivateConnections(ICSSC_DEFAULT)
        if (Connection.Count > 0) and (Connection.Count < 2) then
            for each Item in Connection
                set PrivateConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
            next
        else
            bReturn = FALSE
        end if
    elseif (CONNECTION_ALL = CONNECTION_TYPE) then
        set Connection = NetSharingManager.EnumEveryConnection
        if (Connection.Count > 0) then
            set EveryConnectionCollection = Connection
        else
            bReturn = FALSE
        end if
    else
        bReturn = FALSE
    end if

    if (TRUE = bReturn)  then
        if (Connection.Count = 0) then
            Wscript.Echo("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE)) + " connections exist (Connection.Count gave us 0)")
            bReturn = FALSE
        'valid to have more than 1 connection returned from EnumEveryConnection
        elseif (Connection.Count > 1) and (CONNECTION_ALL <> CONNECTION_TYPE) then
            Wscript.Echo("ERROR: There was more than one " + ConvertConnectionTypeToString(CONNECTION_TYPE) + " connection (" + CStr(Connection.Count) + ")")
            bReturn = FALSE
        end if
    end if
    Wscript.Echo(CStr(Connection.Count) + " objects for connection type " + ConvertConnectionTypeToString(CONNECTION_TYPE))

    GetConnection = bReturn
end function

function ConvertConnectionTypeToString(ConnectionID)
    DIM ConnectionString

    if (ConnectionID = CONNECTION_PUBLIC) then
        ConnectionString = "public"
    elseif (ConnectionID = CONNECTION_PRIVATE) then
        ConnectionString = "private"
    elseif (ConnectionID = CONNECTION_ALL) then
        ConnectionString = "all"
    else
        ConnectionString = "Unknown: " + CStr(ConnectionID)
    end if

    ConvertConnectionTypeToString = ConnectionString
end function



复制以上内容命名为Ics.vbs

用C#代码执行Start.bat之后,在执行脚本Ics.vbs

启用上面的脚本需执行命令cscript /nologo Ics.vbs "虚拟无线网卡名" "本地连接" "on",此时设置共享已完成,

此处执行命令时,请采用多线程的方式



第三种方案

使用微软提供给我们的API,这里我们可以用C语言写,或C++写,当然也可以是C#

C语言可以选择带界面的Win32程序或不带界面的控制台程序

C++可以选用MFC

C#使用WINAPI引进DLL

需要的API函数:

第一个API函数:

DWORD WINAPI WlanOpenHandle(
  __in        DWORD dwClientVersion,
  __reserved  PVOID pReserved,
  __out       PDWORD pdwNegotiatedVersion,
  __out       PHANDLE phClientHandle
);

Parameters
dwClientVersion [in]
Specifies the highest version of the WLAN API that the client supports.

Value        Meaning
1
Client version for Windows XP with SP3 and Wireless LAN API for Windows XP with SP2.

2
Client version for Windows Vista and Windows Server 2008


pReserved
Reserved for future use. Must be set to NULL.

pdwNegotiatedVersion [out]
Specifies the version of the WLAN API that will be used in this session. This value is usually the highest version supported by both the client and server.

phClientHandle [out]
Specifies a handle for the client to use in this session. This handle is used by other functions throughout the session.

Return Value
If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value may be one of the following return codes.

第二个API函数:

DWORD WINAPI WlanHostedNetworkStartUsing(
  __in        HANDLE hClientHandle,
  __out_opt   PWLAN_HOSTED_NETWORK_REASON pFailReason,
  __reserved  PVOID pvReserved
);

Parameters
hClientHandle [in]
The client's session handle, returned by a previous call to the WlanOpenHandle function.

pFailReason [out, optional]
An optional pointer to a value that receives the failure reason, if the call to the WlanHostedNetworkStartUsingfunction fails. Possible values for the failure reason are from the WLAN_HOSTED_NETWORK_REASONenumeration type defined in the Wlanapi.h header file.

pvReserved
Reserved for future use. This parameter must be NULL.

Return Value
If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value may be one of the following return codes.

以上两函数摘自微软的MSDN,由于篇幅关系,这里我不便一一举例,更多API请到Visual studio 2010或更高版本的MSDN中寻找

MFC核心示例代码

ShareNet.h代码

#pragma once

#include
#include
#include

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

class ShareNet
{
public:
ShareNet(void);
~ShareNet(void);
HRESULT AddSharingNet(INetSharingManager * pNSM);
HRESULT StartHostednetwork(void);
void SetNet(CString userName, CString passWord);
};



ShareNet.cpp代码


#include "StdAfx.h"
#include "ShareNet.h"


ShareNet::ShareNet(void)
{
}


ShareNet::~ShareNet(void)
{
}


HRESULT ShareNet::AddSharingNet(INetSharingManager * pNSM)
{
INetConnection * pNC = NULL;
    INetSharingEveryConnectionCollection * pNSECC = NULL;
    HRESULT hr = pNSM->get_EnumEveryConnection (&pNSECC);
    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");
      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"Wireless Network Connection 2")))
      {
       pVWifi = pNP;
       hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC,&pVWifiNSC);
       pVWifiNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE);
      }
     }
    }
   }
   INetSharingPortMapping * pNSPM = NULL;
   pVWifiNSC->Release();
   if (pNSPM)
   {
    hr = pNSPM->Enable();
    INetSharingPortMappingProps* pMapping = NULL;
    pNSPM->get_Properties(&pMapping);
    BSTR result = NULL;
    long port = 0;
    pMapping->get_Name(&result);
    pMapping->get_ExternalPort(&port);
    pNSPM->Release();
   }
}
return hr;
}


HRESULT ShareNet::StartHostednetwork(void)
{
DWORD dwNegotiatedVersion = 1;
HANDLE hClientHandle = 0;
DWORD dwClientVersion = 2;
DWORD hWlan;
DWORD hStart;
WLAN_HOSTED_NETWORK_REASON failReason;

hWlan = WlanOpenHandle(dwClientVersion,NULL,&dwNegotiatedVersion,&hClientHandle);
if(hWlan == ERROR_SUCCESS)
  hStart = WlanHostedNetworkStartUsing (hClientHandle,&failReason,NULL);
return hStart;
}


void ShareNet::SetNet(CString userName, CString passWord)
{
CString seetingCmd = L"cmd /c netsh wlan set hostednetwork ssid=" + userName + L" key=" + passWord;
while (true)
  if ((int)ShellExecute(NULL,NULL,L"cmd.exe",L"cmd /c netsh wlan set hostednetwork mode=allow",NULL,SW_HIDE) > 32) break;
while (true)
  if ((int)ShellExecute(NULL,NULL,L"cmd.exe",seetingCmd,NULL,SW_HIDE) > 32) break;
}
事件代码:



void CAddShareNetDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CString userName,passWord;
GetDlgItemText(IDC_EDIT1,userName);
GetDlgItemText(IDC_EDIT2,passWord);
ShareNet NetStart;
CoInitialize(NULL);
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);
NetStart.SetNet(userName,passWord);
while(true)
  if (NetStart.StartHostednetwork() == ERROR_SUCCESS) break;
NetStart.AddSharingNet(pNSM);
}

此帖出自RF/无线论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
快速回复 返回顶部 返回列表