コード例 #1
0
        /// <summary>
        /// Creates a new instance of a Native Wifi service client.
        /// Throws Win32 errors: ERROR_INVALID_PARAMETER, ERROR_NOT_ENOUGH_MEMORY, RPC_STATUS, ERROR_REMOTE_SESSION_LIMIT_EXCEEDED.
        /// </summary>
        public WlanClient()
        {
            int             errorCode = 0;
            OperatingSystem osInfo    = Environment.OSVersion;

            bool isWinXP =
                osInfo.Platform == PlatformID.Win32NT &&
                osInfo.Version.Major == 5 &&
                osInfo.Version.Minor != 0;

            if (isWinXP && osInfo.ServicePack == "Service Pack 1") // wlanapi not supported in sp1 (or sp2 without hotfix)
            {
                errorCode = NO_WIFI;
            }
            else
            {
                // Perform exception safe init
                // It can be SP2 without hotfix which would generate exception
                try
                {
                    errorCode = WlanInterop.WlanOpenHandle(WlanInterop.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle);
                }
                catch
                {
                    errorCode = NO_WIFI;
                }
            }

            if (errorCode != 0)
            {
                NoWifiAvailable = true;
                return;
            }

            try
            {
                // Interop callback
                wlanNotificationCallback = new WlanInterop.WlanNotificationCallbackDelegate(OnWlanNotification);

                WlanNotificationSource prevSrc;
                WlanInterop.ThrowIfError(WlanInterop.WlanRegisterNotification(clientHandle, WlanNotificationSource.All, false, wlanNotificationCallback, IntPtr.Zero, IntPtr.Zero, out prevSrc));
            }
            catch
            {
                WlanInterop.WlanCloseHandle(clientHandle, IntPtr.Zero);
                throw;
            }
        }