win32API管理无线网络

  许久没登录的百度贴吧,今天偶然去逛了下。私信里面收到了很多小盆友们关于之前一个求学期间写的wifi小工具的一些疑问。顺便一起解答了吧。
  windows建立个人热点后把当前Internet连接共享给你的个人热点。针对我们学校使用电信出品的NetKeeper进行pppoe拨号,首先需要在进程中找到NetKeeper本身,然后利用句柄移除NetKeeper子线程对ICS的占用。不要尝试去挂起主进程,Netkeeper的服务器会与在线的客户端进行心跳包验证。验证失败则会把客户端强制踢下线。

1
2
3
netsh wlan set hostednetwork mode=allow    
netsh wlan set hostednetwork ssid=’testwifi’ key=1234567890    
netsh wlan start hostednetwork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
///   
/// 初始化  
///   

private void Init()    
{    
    try
    {    
        WlanUtils.Throw _On_Win32_Error(wlanapi.WlanOpenHandle(wlanapi.WLAN_CLIENT_VERSION_VISTA, IntPtr.Zero, outthis._ServerVersion, refthis._WlanHandle));    

        WLAN_NOTIFICATION_SOURCE notifSource;    
        WlanUtils.Throw_On_Win32_Error(wlanapi.WlanRegisterNotification(this._WlanHandle, WLAN_NOTIFICATION_SOURCE.All, truethis._notificationCallback, IntPtr.Zero, IntPtr.Zero, out notifSource));    

        WLAN_HOSTED_NETWORK_REASON failReason = this.InitSettings();    
        if (failReason != WLAN_HOSTED_NETWORK_REASON.wlan_hosted_network_reason_success)    
        {    
            thrownew Exception(“Init Error WlanHostedNetworkInitSettings: “ + failReason.ToString());    
        }    

    }    
    catch
    {    
        wlanapi.WlanCloseHandle(this._WlanHandle, IntPtr.Zero);    
        throw;    
    }    
}    

///   
/// 设置SSID及连接数  
///   

public WLAN_HOSTED_NETWORK_REASON SetConnectionSettings(string hostedNetworkSSID, int maxNumberOfPeers)    
{    
    WLAN_HOSTED_NETWORK_REASON failReason;    

    WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS settings = new WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS();    
    settings.hostedNetworkSSID = WlanUtils.ConvertStringToDOT11_SSID(hostedNetworkSSID);    
    settings.dwMaxNumberOfPeers = (uint)maxNumberOfPeers;    

    IntPtr settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(settings));    
    Marshal.StructureToPtr(settings, settingsPtr, false);    

    WlanUtils.Throw_On_Win32_Error(    
        wlanapi.WlanHostedNetworkSetProperty(    
            this._WlanHandle,    
            WLAN_HOSTED_NETWORK_OPCODE.wlan_hosted_network_opcode_connection_settings,    
            (uint)Marshal.SizeOf(settings), settingsPtr, out failReason, IntPtr.Zero    
        )    
    );    

    return failReason;    
}    

///   
/// 设置密码  
///   

public WLAN_HOSTED_NETWORK_REASON SetSecondaryKey(string passKey)    
{    
    WLAN_HOSTED_NETWORK_REASON failReason;    

    WlanUtils.Throw_On_Win32_Error(wlanapi.WlanHostedNetworkSetSecondaryKey(this._WlanHandle, (uint)(passKey.Length + 1), passKey, truetrueout failReason, IntPtr.Zero));    

    return failReason;    
}    

///   
/// 查询状态   
///   

public WLAN_HOSTED_NETWORK_STATUS QueryStatus()    
{    
    WLAN_HOSTED_NETWORK_STATUS status;    
    WlanUtils.Throw_On_Win32_Error(wlanapi.WlanHostedNetworkQueryStatus(this._WlanHandle, out status, IntPtr.Zero));    
    return status;    
}    

///   
/// 启动热点  
///   

public void StartHostedNetwork()    
{    
    try
    {    
        this.ForceStop();    

        var failReason = this.StartUsing();    
        if (failReason != WLAN_HOSTED_NETWORK_REASON.wlan_hosted_network_reason_success)    
        {    
            thrownew Exception(“Could Not Start Hosted Network!nn” + failReason.ToString());    
        }    
    }    
    catch
    {    
        this.ForceStop();    
        throw;    
    }    
}    

///   
/// 设置ICS共享  
///   

public static void EnableInternetConnectionSharing(string Share)    
{    
    try
    {    
        var connectionToShare = Share;    
        var sharedForConnection = “无线网络连接 2”;    

        var manager = new NetSharingManager();    
        var connections = manager.EnumEveryConnection;    

        foreach (INetConnection c in connections)    
        {    
            var props = manager.NetConnectionProps[c];    
            var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];    
            if (props.Name == connectionToShare)    
            {    
                sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);    
            }    
            elseif (props.Name == sharedForConnection)    
            {    
                sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);    
            }    
        }    
    }    
    catch (Exception c)    
    {    
        MessageBox.Show(c.Message);    
    }    
}