예제 #1
0
 /// <summary>
 /// Closes the handle.
 /// </summary>
 private void Close()
 {
     if (clientHandle != IntPtr.Zero)
     {
         Wlan.WlanCloseHandle(clientHandle, IntPtr.Zero);
         clientHandle = IntPtr.Zero;
     }
 }
예제 #2
0
파일: Form1.cs 프로젝트: dfizban/wlanaptest
        private static bool SetRadioState(Guid interfaceGuid, Wlan.Dot11RadioState radioState)
        {
            //set Wlan.WlanPhyRadioState struct state's value.
            var state = new Wlan.WlanPhyRadioState
            {
                dwPhyIndex = (int)Wlan.Dot11PhyType.Any,
                dot11SoftwareRadioState = radioState,
            };
            var size = Marshal.SizeOf(state);

            var pointer = IntPtr.Zero;

            try
            {
                pointer = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(state, pointer, false);

                var clientHandle = IntPtr.Zero;
                try
                {
                    uint negotiatedVersion;
                    var  result = Wlan.WlanOpenHandle(
                        Wlan.WLAN_CLIENT_VERSION_LONGHORN,
                        IntPtr.Zero,
                        out negotiatedVersion,
                        out clientHandle);
                    if (result != 0)
                    {
                        return(false);
                    }

                    result = Wlan.WlanSetInterface(
                        clientHandle,
                        interfaceGuid,
                        Wlan.WlanIntfOpcode.RadioState,
                        (uint)size,
                        pointer,
                        IntPtr.Zero);

                    return(result == 0);
                }
                finally
                {
                    Wlan.WlanCloseHandle(
                        clientHandle,
                        IntPtr.Zero);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(pointer);
            }
        }
예제 #3
0
        private List <Object> GetAvailableNetworkList2()
        {
            WlanClient wlanClient = null;

            WlanClient.WlanInterface wlanInterface = null;
            List <Object>            wlanList      = new List <object>();

            try
            {
                wlanClient = new WlanClient();
            }
            catch (Exception exception)
            {
                Wlan.WlanCloseHandle(clientHandle, IntPtr.Zero);
                LogHelper.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, exception.Message);
                LogHelper.Error(exception);
            }

            if (wlanClient != null && wlanClient.Interfaces.Length != 0)
            {
                wlanInterface = wlanClient.Interfaces[0];
                WlanClient.WlanInterface wlanInterfaceTmp = wlanClient.Interfaces[0];
                Wlan.WlanBssEntry[]      bssworks         = wlanInterface.GetNetworkBssList();
                string temp  = string.Empty;
                int    index = 1;
                foreach (Wlan.WlanBssEntry bsswork in bssworks)
                {
                    bool isConnected = bsswork.inRegDomain;
                    temp = GetMacAddress(bsswork.dot11Bssid);
                    int    rssi   = bsswork.rssi;
                    string apssid = GetStringForSSID(bsswork.dot11Ssid);
                    wlanList.Add(new
                    {
                        IndexID         = index,
                        NetworkName     = GetStringForSSID(bsswork.dot11Ssid),
                        SignalQuality   = bsswork.linkQuality.ToString() + "%",
                        SignalStrength  = StringHelper.ClassifySignalByStrength(Convert.ToInt32(bsswork.linkQuality)),
                        NetworkSecurity = bsswork.inRegDomain ? "安全" : "不安全",
                        NetworkFlag     = Convert.ToInt32(bsswork.chCenterFrequency),
                        Connectable     = bsswork.inRegDomain ? "可连接" : "不可访问",
                        ConnectState    = isConnected ? "已连接使用中" : "未连接",
                        PhysicsId       = bsswork.phyId,
                        Frequency       = bsswork.hostTimestamp
                    });
                    index++;
                }
            }

            return(wlanList);
        }
예제 #4
0
 /// <summary>
 /// Creates a new instance of a Native Wifi service client.
 /// </summary>
 public WlanClient()
 {
     Wlan.ThrowIfError(
         Wlan.WlanOpenHandle(Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle));
     try {
         Wlan.WlanNotificationSource prevSrc;
         wlanNotificationCallback = new Wlan.WlanNotificationCallbackDelegate(OnWlanNotification);
         Wlan.ThrowIfError(Wlan.WlanRegisterVirtualStationNotification(clientHandle, true, IntPtr.Zero));
         Wlan.ThrowIfError(
             Wlan.WlanRegisterNotification(clientHandle, Wlan.WlanNotificationSource.All, false, wlanNotificationCallback, IntPtr.Zero, IntPtr.Zero, out prevSrc));
     } catch {
         Wlan.WlanCloseHandle(clientHandle, IntPtr.Zero);
         throw;
     }
 }
예제 #5
0
        private List <Object> GetAvailableNetworkList1()
        {
            WlanClient wlanClient = null;

            WlanClient.WlanInterface wlanInterface = null;
            List <Object>            wlanList      = null;

            try
            {
                wlanClient = new WlanClient();
            }
            catch (Exception exception)
            {
                Wlan.WlanCloseHandle(clientHandle, IntPtr.Zero);
                LogHelper.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, exception.Message);
                LogHelper.Error(exception);
                MessageBox.Show(exception.Message, "信息", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }

            if (wlanClient != null && wlanClient.Interfaces.Length != 0)
            {
                wlanList      = new List <object>();
                wlanInterface = wlanClient.Interfaces[0];
                WlanClient.WlanInterface    wlanInterfaceTmp  = wlanClient.Interfaces[0];
                Wlan.WlanAvailableNetwork[] availableNetworks = wlanInterfaceTmp.GetAvailableNetworkList(Wlan.WlanGetAvailableNetworkFlags.IncludeAllAdhocProfiles);
                wlanAvailableNetworks = availableNetworks;

                int index = 1;
                foreach (Wlan.WlanAvailableNetwork wlanAvailableNetworkTmp in availableNetworks)
                {
                    bool isConnected = Wlan.WlanAvailableNetworkFlags.Connected.ToString() == wlanAvailableNetworkTmp.flags.ToString().Split(',')[0] && wlanAvailableNetworkTmp.networkConnectable;
                    wlanList.Add(new
                    {
                        IndexID         = index,
                        NetworkName     = GetStringForSSID(wlanAvailableNetworkTmp.dot11Ssid),
                        SignalQuality   = wlanAvailableNetworkTmp.wlanSignalQuality.ToString() + "%",
                        SignalStrength  = StringHelper.ClassifySignalByStrength(Convert.ToInt32(wlanAvailableNetworkTmp.wlanSignalQuality)),
                        NetworkSecurity = wlanAvailableNetworkTmp.securityEnabled ? "安全" : "不安全",
                        NetworkFlag     = Convert.ToInt32(wlanAvailableNetworkTmp.flags),
                        Connectable     = wlanAvailableNetworkTmp.networkConnectable ? "可连接" : "不可访问",
                        ConnectState    = isConnected ? "已连接使用中" : "未连接",
                    });
                    index++;
                }
            }
            return(wlanList);
        }