Exemple #1
0
        /// <summary>
        /// Initializes access point
        /// </summary>
        private void Init()
        {
            try
            {
                WlanUtils.ThrowOnWin32Error(
                    wlanapi.WlanOpenHandle(
                        wlanapi.WLAN_CLIENT_VERSION_VISTA,
                        IntPtr.Zero,
                        out _ServerVersion,
                        ref _WlanHandle));

                WLAN_NOTIFICATION_SOURCE notificationSource;
                WlanUtils.ThrowOnWin32Error(
                    wlanapi.WlanRegisterNotification(
                        _WlanHandle,
                        WLAN_NOTIFICATION_SOURCE.ALL,
                        true,
                        _notificationCallback,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        out notificationSource));

                WLAN_HOSTED_NETWORK_REASON failReason = InitSettings();
                if (failReason != WLAN_HOSTED_NETWORK_REASON.SUCCESS)
                {
                    throw new Exception("Init Error WlanHostedNetworkInitSettings: " + failReason.ToString());
                }
            }
            catch
            {
                wlanapi.WlanCloseHandle(_WlanHandle, IntPtr.Zero);
                throw new Exception("Failed to create instance of hosted network\nMaybe your computer doesn't support hosted network");
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets hosted network parameters
        /// </summary>
        /// <param name="hostedNetworkSSID">SSID</param>
        /// <param name="maxNumberOfPeers">max number of peers</param>
        /// <returns>тип значения</returns>
        public WLAN_OPCODE_VALUE_TYPE QueryConnectionSettings(out string hostedNetworkSSID, out int maxNumberOfPeers)
        {
            uint   dataSize;
            IntPtr dataPtr;
            WLAN_OPCODE_VALUE_TYPE opcode;

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkQueryProperty(
                    _WlanHandle,
                    WLAN_HOSTED_NETWORK_OPCODE.CONNECTION_SETTINGS,
                    out dataSize,
                    out dataPtr,
                    out opcode,
                    IntPtr.Zero));

            var settings =
                (WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS)Marshal.PtrToStructure(
                    dataPtr,
                    typeof(WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS));

            hostedNetworkSSID = settings.hostedNetworkSSID.ucSSID;

            maxNumberOfPeers = (int)settings.dwMaxNumberOfPeers;

            return(opcode);
        }
Exemple #3
0
        /// <summary>
        /// Initializes hosted network settings
        /// </summary>
        /// <returns>fail reason</returns>
        public WLAN_HOSTED_NETWORK_REASON InitSettings()
        {
            WLAN_HOSTED_NETWORK_REASON failReason;

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkInitSettings(
                    _WlanHandle,
                    out failReason,
                    IntPtr.Zero));
            return(failReason);
        }
Exemple #4
0
        /// <summary>
        /// Gets a status of hosted network
        /// </summary>
        /// <returns>hosted network status</returns>
        public WLAN_HOSTED_NETWORK_STATUS QueryStatus()
        {
            IntPtr ptr = new IntPtr();

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkQueryStatus(
                    _WlanHandle,
                    out ptr,
                    IntPtr.Zero));

            var status = (WLAN_HOSTED_NETWORK_STATUS)Marshal.PtrToStructure(ptr, typeof(WLAN_HOSTED_NETWORK_STATUS));

            return(status);
        }
Exemple #5
0
        /// <summary>
        /// Sets a password of hosted network
        /// </summary>
        /// <param name="passKey">password</param>
        /// <returns>fail reason</returns>
        public WLAN_HOSTED_NETWORK_REASON SetSecondaryKey(string passKey)
        {
            WLAN_HOSTED_NETWORK_REASON failReason;

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkSetSecondaryKey(
                    _WlanHandle,
                    (uint)(passKey.Length + 1),
                    passKey,
                    true,
                    true,
                    out failReason,
                    IntPtr.Zero));
            return(failReason);
        }
Exemple #6
0
        /// <summary>
        /// Gets AP's password
        /// </summary>
        /// <param name="passKey">password of hosted network</param>
        /// <param name="isPassPhrase">indicates if the key data is in passphrase format</param>
        /// <param name="isPersistent">indicates if the key data is to be stored and reused later or is for one-time use only</param>
        /// <returns>fail reason</returns>
        public WLAN_HOSTED_NETWORK_REASON QuerySecondaryKey(out string passKey, out bool isPassPhrase, out bool isPersistent)
        {
            WLAN_HOSTED_NETWORK_REASON failReason;
            uint keyLen;

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkQuerySecondaryKey(
                    _WlanHandle,
                    out keyLen,
                    out passKey,
                    out isPassPhrase,
                    out isPersistent,
                    out failReason,
                    IntPtr.Zero));
            return(failReason);
        }
Exemple #7
0
        /// <summary>
        /// Sets hosted network parameters
        /// </summary>
        /// <param name="hostedNetworkSSID">SSID</param>
        /// <param name="maxNumberOfPeers">max number of peers</param>
        /// <returns>fail reason</returns>
        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  = hostedNetworkSSID.ToDOT11_SSID();
            settings.dwMaxNumberOfPeers = maxNumberOfPeers;

            IntPtr settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(settings));

            Marshal.StructureToPtr(settings, settingsPtr, true);

            WlanUtils.ThrowOnWin32Error(
                wlanapi.WlanHostedNetworkSetProperty(
                    _WlanHandle,
                    WLAN_HOSTED_NETWORK_OPCODE.CONNECTION_SETTINGS,
                    (uint)Marshal.SizeOf(settings),
                    settingsPtr,
                    out failReason,
                    IntPtr.Zero));
            return(failReason);
        }