private void setNetworkSettings(string ssid, int maxPeers) { NativeWLanAPI._DOT11_SSID ossid = new NativeWLanAPI._DOT11_SSID(); NativeWLanAPI._WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS settings = new NativeWLanAPI._WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS(); if (ssid.Length > 32) { throw new ArgumentException("Wi-Fi名称必须小于32个字符"); } else { ossid.uSSIDLength = (uint)ssid.Length; ossid.ucSSID = ssid; settings.hostedNetworkSSID = ossid; } if (maxPeers == 0) { throw new ArgumentException("Max number of peers must be greater than 0"); } settings.dwMaxNumberOfPeers = (uint)maxPeers; int size = Marshal.SizeOf(settings); IntPtr pSettings = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(settings, pSettings, true); NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE opcode = NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE._WLAN_HOSTED_NETWORK_OPCODE_connection_settings; NativeWLanAPI.WlanHostedNetworkSetProperty(handle, opcode, (uint)size, pSettings, out lastError, IntPtr.Zero); checkStatusAndThrow("设置Wi-Fi名称失败"); Marshal.FreeHGlobal(pSettings); }
private bool initSettings() { lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkInitSettings(handle, out lastError, IntPtr.Zero); checkStatusAndThrow("Didnt initialize settings correctly", true); return(true); }
public List <ArpIpAddress> GetListArp() { List <NativeWLanAPI.MIB_IPNETROW> ArpList = new List <NativeWLanAPI.MIB_IPNETROW>(); List <ArpIpAddress> addresses = new List <ArpIpAddress>(); int size = 0; NativeWLanAPI.GetIpNetTable(IntPtr.Zero, ref size, true); IntPtr ipList = Marshal.AllocHGlobal(size); if ((int)NativeWLanAPI.GetIpNetTable(ipList, ref size, true) == 0) { var num = Marshal.ReadInt32(ipList); var ptr = IntPtr.Add(ipList, 4); for (int i = 0; i < num; i++) { ArpList.Add((NativeWLanAPI.MIB_IPNETROW)Marshal.PtrToStructure(ptr, typeof(NativeWLanAPI.MIB_IPNETROW))); ptr = IntPtr.Add(ptr, Marshal.SizeOf(typeof(NativeWLanAPI.MIB_IPNETROW))); } Marshal.FreeHGlobal(ipList); } foreach (var item in ArpList) { addresses.Add(new ArpIpAddress() { IpAddress = ArpHelper.ConvertIpToStr(item.Addr), MacAddress = ArpHelper.CovnertMacToStr(item.PhysAddr) }); } return(addresses); }
private bool openHandle(int serverVersion) { lastErrorCode = (int)NativeWLanAPI.WlanOpenHandle((uint)serverVersion, IntPtr.Zero, out version, out handle); checkStatusAndThrow("Couldn't retrive handle to SoftAP"); return(handleConnected = true); }
private void setPassword(string pass) { if (pass.Length < 8 || pass.Length > 63) { throw new ArgumentException("密码必须在8到63个字符之间"); } NativeWLanAPI.WlanHostedNetworkSetSecondaryKey(handle, (uint)(pass.Length + 1), pass, true, true, out lastError, IntPtr.Zero); checkStatusAndThrow("设置密码失败"); }
/// <summary> /// Stops the Software Access Point /// </summary> /// <returns>Returns True if the AP has been stopped</returns> public bool Stop() { if (serverRunning) { lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkForceStop(handle, out lastError, IntPtr.Zero); checkStatusAndThrow("Could not stop SoftAP"); serverRunning = false; } return(true); }
/// <summary> /// Starts the Software Access Point /// </summary> /// <returns>True if the server has been started</returns> public bool Start() { NativeWLanAPI.WlanHostedNetworkStartUsing(handle, out lastError, IntPtr.Zero); checkStatusAndThrow("启动Wi-Fi失败"); NativeWLanAPI.WlanHostedNetworkForceStart(handle, out lastError, IntPtr.Zero); checkStatusAndThrow("启动Wi-Fi失败"); WlanShare.ShareStart(); return(serverRunning = true); }
/// <summary> /// Enables the Software AP. Sometimes when Start() won't work, Enable() and then Start() might work. /// </summary> public void Enable() { NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE opcode = NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE._WLAN_HOSTED_NETWORK_OPCODE_enable; int size = Marshal.SizeOf(new Int32()); IntPtr setting = Marshal.AllocHGlobal(size); Marshal.WriteInt32(setting, 1); lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkSetProperty(handle, opcode, (uint)size, setting, out lastError, IntPtr.Zero); Marshal.FreeHGlobal(setting); checkStatusAndThrow("Could not enable Hosted Access Point! "); }
/// <summary> /// Query a status of Software Access Point /// </summary> /// <returns>A construct with status of the AP</returns> public HostedNetworkStatus getStatus() { var addressList = GetListArp(); IntPtr ppStatus = IntPtr.Zero; lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkQueryStatus(handle, out ppStatus, IntPtr.Zero); checkStatusAndThrow("Could not retrive SoftAP status", false); NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS qstat = (NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ppStatus, typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS)); bool isActive = qstat.HostedNetworkState == NativeWLanAPI._WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active ? true : false; int peersConnected = (int)qstat.dwNumberOfPeers; Guid guid = qstat.IPDeviceID; string networkMac = BitConverter.ToString(qstat.wlanHostedNetworkBSSID.address);//网卡地址 LinkedList <HostedNetworkPeer> peers = new LinkedList <HostedNetworkPeer>(); IntPtr offset = Marshal.OffsetOf(typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_STATUS), "PeerList"); for (int i = 0; i < peersConnected; i++) { IntPtr ppeer = new IntPtr(ppStatus.ToInt64() + offset.ToInt64()); NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE peer = (NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE)Marshal.PtrToStructure(ppeer, typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_PEER_STATE)); string mac = BitConverter.ToString(peer.PeerMacAddress.address); bool authenticated = ((int)peer.PeerAuthState == 1 ? true : false); string ipAddress = addressList.LastOrDefault(x => string.Equals(x.MacAddress, mac, StringComparison.CurrentCultureIgnoreCase))?.IpAddress; HostedNetworkPeer hpeer = new HostedNetworkPeer(mac, authenticated, DnsHelper.GetHostName(ipAddress), ipAddress); peers.AddLast(hpeer); offset += Marshal.SizeOf(peer); } NativeWLanAPI.WlanFreeMemory(ppStatus); return(new HostedNetworkStatus(isActive, guid, peersConnected, peers, networkMac)); }
/// <summary> /// Queries the settings of Software Access Point /// </summary> /// <returns>Current settings of the AP</returns> public HostedNetworkSettings getSettings() { UInt32 pdwDataSize = new UInt32(); IntPtr ppvData = IntPtr.Zero; NativeWLanAPI._WLAN_OPCODE_VALUE_TYPE pWlanOpcodeValueType = new NativeWLanAPI._WLAN_OPCODE_VALUE_TYPE(); NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE opcode = NativeWLanAPI._WLAN_HOSTED_NETWORK_OPCODE._WLAN_HOSTED_NETWORK_OPCODE_connection_settings; lastErrorCode = (int)NativeWLanAPI.WlanHostedNetworkQueryProperty(handle, opcode, out pdwDataSize, out ppvData, out pWlanOpcodeValueType, IntPtr.Zero); checkStatusAndThrow("Could not get network settings at this time"); NativeWLanAPI._WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS qsettings = (NativeWLanAPI._WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS)Marshal.PtrToStructure(ppvData, typeof(NativeWLanAPI._WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS)); int maxPeers = (int)qsettings.dwMaxNumberOfPeers; string ssid = qsettings.hostedNetworkSSID.ucSSID; string password = getPassword(); NativeWLanAPI.WlanFreeMemory(ppvData); return(new HostedNetworkSettings(ssid, password, maxPeers)); }
private string getPassword() { string pass = string.Empty; bool persistent; bool passphrase; uint length; IntPtr ppass = IntPtr.Zero; int error = (int)NativeWLanAPI.WlanHostedNetworkQuerySecondaryKey(handle, out length, out ppass, out passphrase, out persistent, out lastError, IntPtr.Zero); if (error != 0) { throw new ApplicationException("Retriving password failed!"); } else { if (length != 0 && ppass != IntPtr.Zero) { pass = Marshal.PtrToStringAnsi(ppass, (int)length); NativeWLanAPI.WlanFreeMemory(ppass); } } return(pass); }