internal static extern uint WlanHostedNetworkQueryProperty( [In] WlanHandle clientHandle, [In] WlanHostedNetworkOpcode opCode, [Out] out uint dataSize, [Out] out IntPtr data, [Out] out WlanOpcodeValueType wlanOpcodeValueType, IntPtr reserved);
internal static extern uint WlanHostedNetworkSetProperty( [In] WlanHandle clientHandle, [In] WlanHostedNetworkOpcode opCode, uint dataSize, IntPtr data, [Out] out WlanHostedNetworkReason failReason, IntPtr reserved);
internal static extern uint WlanHostedNetworkQuerySecondaryKey( [In] WlanHandle clientHandle, [Out] out uint keyLength, [Out] out IntPtr keyData, [Out] out bool isPassPhrase, [Out] out bool isPersistent, [Out] out WlanHostedNetworkReason failReason, IntPtr reserved);
internal static extern uint WlanRegisterNotification( [In] WlanHandle clientHandle, [In] WlanNotificationSource notificationSource, [In] bool ignoreDuplicate, [In] WlanNotificationCallback notificationCallback, [In] IntPtr callbackContext, IntPtr reserved, [Out] out WlanNotificationSource previousNotificationSource);
internal static extern uint WlanOpenHandle( [In] WlanApiVersion clientVersion, IntPtr reserved, [Out] out uint negotiatedVersion, out WlanHandle clientHandle);
internal static extern uint WlanHostedNetworkQueryStatus( [In] WlanHandle clientHandle, [Out] out IntPtr wlanHostedNetworkStatus, IntPtr reserved);
internal static extern uint WlanHostedNetworkInitSettings( [In] WlanHandle clientHandle, [Out] out WlanHostedNetworkReason failReason, IntPtr reserved);
internal static extern uint WlanHostedNetworkForceStop( [In] WlanHandle clientHandle, [Out] out WlanHostedNetworkReason failReason, IntPtr reserved);
public HostedNetworkManager() { var enabled = IntPtr.Zero; var connectionSettings = IntPtr.Zero; var securitySettings = IntPtr.Zero; var status = IntPtr.Zero; try { Logger.Trace(".ctor: Start invoking native codes..."); Lock(); uint negotiatedVersion; WlanHandle clientHandle; Logger.Trace(".ctor: Invoking WlanOpenHandle..."); var returnValue = NativeMethods.WlanOpenHandle( WlanApiVersion.Version, IntPtr.Zero, out negotiatedVersion, out clientHandle); Logger.Trace(".ctor: WlanOpenHandle returned {0}", returnValue); Utilities.ThrowOnError(returnValue); if (negotiatedVersion != (uint) WlanApiVersion.Version) { Logger.Error(".ctor: Wlan API version negotiation failed"); throw new WlanException("Wlan API version negotiation failed"); } Logger.Trace(".ctor: _wlanHandle: {0:x16}", clientHandle.DangerousGetHandle().ToInt64()); this._wlanHandle = clientHandle; WlanNotificationSource previousNotificationSource; Logger.Trace(".ctor: Invoking WlanRegisterNotification..."); returnValue = NativeMethods.WlanRegisterNotification( clientHandle, WlanNotificationSource.HostedNetwork, true, OnNotification, IntPtr.Zero, IntPtr.Zero, out previousNotificationSource); Logger.Trace(".ctor: WlanRegisterNotification returned {0}", returnValue); Utilities.ThrowOnError(returnValue); WlanHostedNetworkReason faileReason; Logger.Trace(".ctor: Invoking WlanHostedNetworkInitSettings..."); returnValue = NativeMethods.WlanHostedNetworkInitSettings( clientHandle, out faileReason, IntPtr.Zero); Logger.Trace(".ctor: WlanHostedNetworkInitSettings returned {0}", returnValue); Utilities.ThrowOnError(returnValue); uint dataSize; WlanOpcodeValueType opcodeValueType; Logger.Trace(".ctor: Invoking WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.Enable..."); returnValue = NativeMethods.WlanHostedNetworkQueryProperty( clientHandle, WlanHostedNetworkOpcode.Enable, out dataSize, out enabled, out opcodeValueType, IntPtr.Zero); Logger.Trace(".ctor: WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.Enable returned {0}", returnValue); Utilities.ThrowOnError(returnValue); this.IsHostedNetworkAllowed = Convert.ToBoolean(Marshal.ReadInt32(enabled)); Logger.Info(".ctor: IsHostedNetworkAllowed: {0}", this.IsHostedNetworkAllowed); Logger.Trace( ".ctor: Invoking WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.ConnectionSettings..."); returnValue = NativeMethods.WlanHostedNetworkQueryProperty( clientHandle, WlanHostedNetworkOpcode.ConnectionSettings, out dataSize, out connectionSettings, out opcodeValueType, IntPtr.Zero); Utilities.ThrowOnError(returnValue); Logger.Trace( ".ctor: WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.ConnectionSettings returned {0}", returnValue); if (connectionSettings == IntPtr.Zero || Marshal.SizeOf(typeof (WlanHostedNetworkConnectionSettings)) < dataSize) { Logger.Error(".ctor: Got invalid connection setting data"); Utilities.ThrowOnError(13); } this._connectionSettings = (WlanHostedNetworkConnectionSettings) Marshal.PtrToStructure(connectionSettings, typeof (WlanHostedNetworkConnectionSettings)); Logger.Info(".ctor: Connection setting: SSID: {0}, max peer count: {1}", this._connectionSettings.HostedNetworkSSID.SSID, this._connectionSettings.MaxNumberOfPeers); Logger.Trace( ".ctor: Invoking WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.SecuritySettings..."); returnValue = NativeMethods.WlanHostedNetworkQueryProperty( clientHandle, WlanHostedNetworkOpcode.SecuritySettings, out dataSize, out securitySettings, out opcodeValueType, IntPtr.Zero); Logger.Trace( ".ctor: WlanHostedNetworkQueryProperty with WlanHostedNetworkOpcode.ConnectionSettings returned {0}", returnValue); if (securitySettings == IntPtr.Zero || Marshal.SizeOf(typeof (WlanHostedNetworkSecuritySettings)) < dataSize) { Logger.Error(".ctor: Got invalid security setting data"); Utilities.ThrowOnError(13); } this._securitySettings = (WlanHostedNetworkSecuritySettings) Marshal.PtrToStructure(securitySettings, typeof (WlanHostedNetworkSecuritySettings)); Logger.Info(".ctor: Security setting: Authentication algorithm: {0}, cipher algorithm: {1}", this._securitySettings.Dot11AuthAlgo, this._securitySettings.Dot11CipherAlgo); Logger.Trace(".ctor: Invoking WlanHostedNetworkQueryStatus..."); returnValue = NativeMethods.WlanHostedNetworkQueryStatus( clientHandle, out status, IntPtr.Zero); Logger.Trace(".ctor: WlanHostedNetworkQueryStatus returned {0}", returnValue); Utilities.ThrowOnError(returnValue); var wlanHostedNetworkStatus = (WlanHostedNetworkStatus) Marshal.PtrToStructure(status, typeof (WlanHostedNetworkStatus)); _hostedNetworkInterfaceGuid = wlanHostedNetworkStatus.IPDeviceID; _hostedNetworkState = wlanHostedNetworkStatus.HostedNetworkState; Logger.Info( ".ctor: Hosted network status: State: {0}, BSSID: {1}, physical type: {2}, channel frequency: {3}, current number of peers: {4}", wlanHostedNetworkStatus.HostedNetworkState, BitConverter.ToString(wlanHostedNetworkStatus.WlanHostedNetworkBSSID), wlanHostedNetworkStatus.Dot11PhyType, wlanHostedNetworkStatus.ChannelFrequency, wlanHostedNetworkStatus.NumberOfPeers); _icsManager = new ICSManager(); Logger.Trace(".ctor: ICSManager initialized "); } catch (ICSException) { Logger.Trace(".ctor: ICSManager is invalid"); } catch (Win32Exception) { if (!this._wlanHandle.IsInvalid) { this._wlanHandle.Dispose(); } throw; } finally { Unlock(); if (enabled != IntPtr.Zero) { NativeMethods.WlanFreeMemory(enabled); } if (connectionSettings != IntPtr.Zero) { NativeMethods.WlanFreeMemory(connectionSettings); } if (securitySettings != IntPtr.Zero) { NativeMethods.WlanFreeMemory(securitySettings); } if (status != IntPtr.Zero) { NativeMethods.WlanFreeMemory(status); } } }