English 中文(简体)
Windows 7 Virtual WiFi using C#?
原标题:

Windows 7 introduced Virtual WiFi which allows you to create hotspots. However I can t find any tutorials on doing it in C#. I found Virtual Router (It is open source and is written in C#) but I can t seem to figure out how it works because it has a lot of unrelated code since it is implemented as a service.

Can anyone explain how can I create a hotspot and assign IP addresses to clients? I don t need features like ICS but I want to be able to broadcast gateway and DNS information.

There is also a closed source alternative called Connectify. I did manage to get its source but it didn t help much. It uses an open source library but I don t know how to create hotspots with it.

最佳回答

Have you thought about looking into this Code-Plex project Virtual Router?

问题回答

Since you ve found a project that does exactly what you want, why not work on understanding that project?

It looks like most of the code you re interested in is in the "VirtualRouter.Wlan" project. Start there and try asking specific questions if you don t understand it.

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Diagnostics;
        using System.Security.Principal;

        namespace WifiRouter
        {
            public partial class Form1 : Form
            {
                bool connect = false;
                public Form1()
                {

                    InitializeComponent();
                }

                public static bool IsAdmin()
                {
                    WindowsIdentity id = WindowsIdentity.GetCurrent();
                    WindowsPrincipal p = new WindowsPrincipal(id);
                    return p.IsInRole(WindowsBuiltInRole.Administrator);
                }
                public void RestartElevated()
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.UseShellExecute = true;
                    startInfo.CreateNoWindow = true;
                    startInfo.WorkingDirectory = Environment.CurrentDirectory;
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                    startInfo.Verb = "runas";
                    try
                    {
                        Process p = Process.Start(startInfo);
                    }
                    catch
                    {

                    }

                    System.Windows.Forms.Application.Exit();
                }
                private void button1_Click(object sender, EventArgs e)
                {
                    string ssid = textBox1.Text, key = textBox2.Text;
                    if (!connect)
                    {
                        if (String.IsNullOrEmpty(textBox1.Text))
                        {
                            MessageBox.Show("SSID cannot be left blank !",
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {

                            if (textBox2.Text == null || textBox2.Text == "")
                            {
                                MessageBox.Show("Key value cannot be left blank !",
                                "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                if (key.Length >= 6)
                                {
                                    Hotspot(ssid, key, true);
                                    textBox1.Enabled = false;
                                    textBox2.Enabled = false;
                                    button1.Text = "Stop";
                                    connect = true;
                                }
                                else
                                {
                                    MessageBox.Show("Key should be more then or Equal to 6 Characters !",
                                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                            }
                        }
                    }
                    else
                    {
                        Hotspot(null, null, false);
                        textBox1.Enabled = true;
                        textBox2.Enabled = true;
                        button1.Text = "Start";
                        connect = false;
                    }
                }
                private void Hotspot(string ssid, string key,bool status)
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
                    processStartInfo.RedirectStandardInput = true;
                    processStartInfo.RedirectStandardOutput = true;
                    processStartInfo.CreateNoWindow = true;
                    processStartInfo.UseShellExecute = false;
                    Process process = Process.Start(processStartInfo);

                    if (process != null)
                    {
                        if (status)
                        {
                            process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow ssid=" + ssid + " key=" + key);
                            process.StandardInput.WriteLine("netsh wlan start hosted network");
                            process.StandardInput.Close();
                        }
                        else
                        {
                            process.StandardInput.WriteLine("netsh wlan stop hostednetwork");
                            process.StandardInput.Close();
                        }
                    }
                }

                private void Form1_Load(object sender, EventArgs e)
                {
                    if (!IsAdmin())
                    {
                        RestartElevated();
                    }
                }

                private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                {
                    Hotspot(null, null, false);
                    Application.Exit();
                }
            }
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签