This is not something new, the same kind of software has a lot, but not the fee is advertising, in school when you want to write a, but then did not learn, the mastery of C is defined as a number of variables, print a line of "Hello,world" so, in order to write this crap, Also deliberately run the library to see a few days of VB, and then online copy some code, with the way of the CMD to achieve the basic functions. I've been doing it for 1 years now. NET code Nong, intends to re-masturbate one, Windows should be open to this API, estimation is also simple.
I searched it before I started, and it didn't seem to work. NET write, it should have been written earlier just did not send out. The only thing that was found was an open source project on CodePlex virtual Router, so it used some of its code.
Here's the final program.
The interface is down, we should not care about these details, to see the implementation process.
Detailed documentation on the virtual WiFi is available on MSDN, which describes some concepts and the use of related functions. Address
The Windows API is written in C, C # calls C to use PInvoke (platform invoke) knowledge, the main related classes are:
- DllImportAttribute: Represents a method from a function in an externally unmanaged DLL
- INTPTR: Used to denote a pointer (weaker than a pointer in C)
- Marshal: A tool class that provides a number of static methods for manipulating unmanaged memory
Because the Windows API defines a large number of structures and constants, which cannot be used directly in C #, it is cumbersome to redefine it in C #, but with pinvoke.net, life is much happier.
But there's a little bit of a problem in the process, and a struct has an array member, like this:
Public Wlan_hosted_network_peer_state[] peerlist;
This object is taken from the pointer parameter of a function, which is always null when the struct is returned by a function. In C you can think of the array as a pointer to the first address, so I changed the member to the IntPtr type, but it was a negative value, shit! Ask the next Valley elder brother, see on StackOverflow on the same question, the answer is: since there is the first address of this structure object, because the members in memory are in order layout, so the pointer is shifted to the peerlist member of the place can be, So I added the following method to this structure:
Public void fixpeerlistaddr (IntPtr ptr) { // offset pointer to wlan_hosted_network_status first address to peerlist member's Place IntPtr offset = marshal.offsetof (typeof"peerlist"); = ptr + offset. ToInt32 (); }
The problem is done!
How to use this IntPtr object to traverse the array it points to, it is very simple in C, the direct for loop inside the peerlist[offset] or * (peerlist + offset), and the IntPtr object is just a representation of the pointer, and there is no strong use of C, You can only increment the offset each time and then call Marshal.PtrToStructure to take the object pointed to by the pointer, given that many places will do so, to simplify the code, I have added two extension methods for IntPtr, as follows:
1 Public Static classsystemextension2 {3 /// <summary>4 ///enumerate all elements based on the first address of the array5 /// </summary>6 /// <typeparam name= "T" ></typeparam>7 /// <param name= "self" ></param>8 /// <param name= "Length" ></param>9 /// <returns></returns>Ten Public StaticIenumerable<t> asenumerable<t> ( ThisIntPtr Self,intlength) One { A intSize = Marshal.SizeOf (typeof(T)); - for(inti =0; i < length; ++i) { - yield returnSelf. Dereference<t>(); theSelf + =size; - } - } - + /// <summary> - ///gets the object that the pointer points to + /// </summary> A /// <param name= "self" ></param> at /// <returns></returns> - Public StaticT dereference<t> ( ThisIntPtr self) - { - returnT Marshal.PtrToStructure (Self,typeof(T)); - } -}
Very simple a small thing, so much, the program has a lot to improve the place, we have any ideas to welcome exchanges, but also let me learn. the first time to write a blog, slag one, but also look at your predecessors a lot of advice!
SOURCE download
Use WinForm to write a virtual WiFi assistant to play (with source)