Esempio n. 1
0
        public static WLAN_CONNECTION_ATTRIBUTES GetConnectionAttributes(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var queryData = IntPtr.Zero;

            try
            {
                var result = WlanQueryInterface(
                    clientHandle,
                    interfaceId,
                    WLAN_INTF_OPCODE.wlan_intf_opcode_current_connection,
                    IntPtr.Zero,
                    out _,
                    out queryData,
                    IntPtr.Zero);

                // ERROR_INVALID_STATE will be returned if the client is not connected to a network.
                return(CheckResult(nameof(WlanQueryInterface), result, false)
                                        ? Marshal.PtrToStructure <WLAN_CONNECTION_ATTRIBUTES>(queryData)
                                        : default(WLAN_CONNECTION_ATTRIBUTES));
            }
            finally
            {
                if (queryData != IntPtr.Zero)
                {
                    WlanFreeMemory(queryData);
                }
            }
        }
Esempio n. 2
0
        public static IEnumerable <WLAN_BSS_ENTRY> GetNetworkBssEntryList(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var wlanBssList = IntPtr.Zero;

            try
            {
                var result = WlanGetNetworkBssList(
                    clientHandle,
                    interfaceId,
                    IntPtr.Zero,
                    DOT11_BSS_TYPE.dot11_BSS_type_any,
                    false,
                    IntPtr.Zero,
                    out wlanBssList);

                // ERROR_NDIS_DOT11_POWER_STATE_INVALID will be returned if the interface is turned off.
                return(CheckResult(nameof(WlanGetNetworkBssList), result, false)
                                        ? new WLAN_BSS_LIST(wlanBssList).wlanBssEntries
                                        : new WLAN_BSS_ENTRY[0]);
            }
            finally
            {
                if (wlanBssList != IntPtr.Zero)
                {
                    WlanFreeMemory(wlanBssList);
                }
            }
        }
Esempio n. 3
0
        public static IEnumerable <WLAN_PHY_RADIO_STATE> GetPhyRadioStates(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var queryData = IntPtr.Zero;

            try
            {
                var result = WlanQueryInterface(
                    clientHandle,
                    interfaceId,
                    WLAN_INTF_OPCODE.wlan_intf_opcode_radio_state,
                    IntPtr.Zero,
                    out _,
                    out queryData,
                    IntPtr.Zero);

                return(CheckResult(nameof(WlanQueryInterface), result, false)
                                        ? new WLAN_RADIO_STATE(queryData).PhyRadioState
                                        : new WLAN_PHY_RADIO_STATE[0]);
            }
            finally
            {
                if (queryData != IntPtr.Zero)
                {
                    WlanFreeMemory(queryData);
                }
            }
        }
Esempio n. 4
0
 public static extern uint WlanSetInterface(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     WLAN_INTF_OPCODE OpCode,
     uint dwDataSize,
     IntPtr pData,             // Pointer to data to be set
     IntPtr pReserved);
Esempio n. 5
0
        public static bool SetPhyRadioState(SafeClientHandle clientHandle, Guid interfaceId, WLAN_PHY_RADIO_STATE state)
        {
            var size = Marshal.SizeOf(state);

            IntPtr setData = IntPtr.Zero;

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

                var result = WlanSetInterface(
                    clientHandle,
                    interfaceId,
                    WLAN_INTF_OPCODE.wlan_intf_opcode_radio_state,
                    (uint)size,
                    setData,
                    IntPtr.Zero);

                // ERROR_ACCESS_DENIED will be thrown if the caller does not have sufficient permissions.
                // By default, only a user who is logged on as a member of the Administrators group or
                // the Network Configuration Operators group can set the operation mode of the interface.
                // ERROR_GEN_FAILURE will be thrown if the OpCode is not supported by the driver or NIC.
                return(CheckResult(nameof(WlanSetInterface), result, false));
            }
            finally
            {
                Marshal.FreeHGlobal(setData);
            }
        }
Esempio n. 6
0
        private static int?GetInterfaceInt(SafeClientHandle clientHandle, Guid interfaceId, WLAN_INTF_OPCODE wlanIntfOpcode)
        {
            var queryData = IntPtr.Zero;

            try
            {
                var result = WlanQueryInterface(
                    clientHandle,
                    interfaceId,
                    wlanIntfOpcode,
                    IntPtr.Zero,
                    out _,
                    out queryData,
                    IntPtr.Zero);

                return(CheckResult(nameof(WlanQueryInterface), result, false)
                                        ? Marshal.ReadInt32(queryData)
                                        : (int?)null);
            }
            finally
            {
                if (queryData != IntPtr.Zero)
                {
                    WlanFreeMemory(queryData);
                }
            }
        }
Esempio n. 7
0
        public static IEnumerable <WLAN_AVAILABLE_NETWORK> GetAvailableNetworkList(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var availableNetworkList = IntPtr.Zero;

            try
            {
                var result = WlanGetAvailableNetworkList(
                    clientHandle,
                    interfaceId,
                    WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES,
                    IntPtr.Zero,
                    out availableNetworkList);

                // ERROR_NDIS_DOT11_POWER_STATE_INVALID will be returned if the interface is turned off.
                return(CheckResult(nameof(WlanGetAvailableNetworkList), result, false)
                                        ? new WLAN_AVAILABLE_NETWORK_LIST(availableNetworkList).Network
                                        : new WLAN_AVAILABLE_NETWORK[0]);
            }
            finally
            {
                if (availableNetworkList != IntPtr.Zero)
                {
                    WlanFreeMemory(availableNetworkList);
                }
            }
        }
Esempio n. 8
0
        public static WLAN_BSS_ENTRY[] GetNetworkBssEntryList(SafeClientHandle clientHandle, Guid interfaceId, DOT11_SSID ssid, DOT11_BSS_TYPE bssType, bool isSecurityEnabled)
        {
            var queryData   = IntPtr.Zero;
            var wlanBssList = IntPtr.Zero;

            try
            {
                queryData = Marshal.AllocHGlobal(Marshal.SizeOf(ssid));
                Marshal.StructureToPtr(ssid, queryData, false);

                var result = WlanGetNetworkBssList(
                    clientHandle,
                    interfaceId,
                    queryData,
                    bssType,
                    isSecurityEnabled,
                    IntPtr.Zero,
                    out wlanBssList);

                // ERROR_NDIS_DOT11_POWER_STATE_INVALID will be returned if the interface is turned off.
                return(CheckResult(nameof(WlanGetNetworkBssList), result, false)
                                        ? new WLAN_BSS_LIST(wlanBssList).wlanBssEntries
                                        : new WLAN_BSS_ENTRY[0]);
            }
            finally
            {
                Marshal.FreeHGlobal(queryData);

                if (wlanBssList != IntPtr.Zero)
                {
                    WlanFreeMemory(wlanBssList);
                }
            }
        }
Esempio n. 9
0
 public static extern uint WlanRegisterNotification(
     SafeClientHandle hClientHandle,
     uint dwNotifSource,
     [MarshalAs(UnmanagedType.Bool)] bool bIgnoreDuplicate,
     WLAN_NOTIFICATION_CALLBACK funcCallback,
     IntPtr pCallbackContext,
     IntPtr pReserved,
     uint pdwPrevNotifSource);
Esempio n. 10
0
 public static extern uint WlanGetProfile(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     [MarshalAs(UnmanagedType.LPWStr)] string strProfileName,
     IntPtr pReserved,
     [MarshalAs(UnmanagedType.LPWStr)] out string pstrProfileXml,
     ref uint pdwFlags,
     out uint pdwGrantedAccess);
Esempio n. 11
0
 public static extern uint WlanGetNetworkBssList(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     IntPtr pDot11Ssid,
     DOT11_BSS_TYPE dot11BssType,
     [MarshalAs(UnmanagedType.Bool)] bool bSecurityEnabled,
     IntPtr pReserved,
     out IntPtr ppWlanBssList);             // Pointer to WLAN_BSS_LIST
Esempio n. 12
0
        public static bool?GetAutoConfig(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var value = GetInterfaceInt(clientHandle, interfaceId, WLAN_INTF_OPCODE.wlan_intf_opcode_autoconf_enabled);

            return(value.HasValue
                                ? (value.Value != 0) // True = other than 0. False = 0.
                                : (bool?)null);
        }
Esempio n. 13
0
 public static extern uint WlanQueryInterface(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     WLAN_INTF_OPCODE OpCode,
     IntPtr pReserved,
     out uint pdwDataSize,
     out IntPtr ppData,             // Pointer to queried data
     IntPtr pWlanOpcodeValueType);
Esempio n. 14
0
 public static extern uint WlanSetProfile(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     uint dwFlags,
     [MarshalAs(UnmanagedType.LPWStr)] string strProfileXml,
     [MarshalAs(UnmanagedType.LPWStr)] string strAllUserProfileSecurity,
     [MarshalAs(UnmanagedType.Bool)] bool bOverwrite,
     IntPtr pReserved,
     out uint pdwReasonCode);             // WLAN_REASON_CODE
Esempio n. 15
0
        public static bool Disconnect(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var result = WlanDisconnect(
                clientHandle,
                interfaceId,
                IntPtr.Zero);

            // ERROR_NOT_FOUND will be returned if the interface is removed.
            return(CheckResult(nameof(WlanDisconnect), result, false));
        }
Esempio n. 16
0
        public static bool DeleteProfile(SafeClientHandle clientHandle, Guid interfaceId, string profileName)
        {
            var result = WlanDeleteProfile(
                clientHandle,
                interfaceId,
                profileName,
                IntPtr.Zero);

            // ERROR_INVALID_PARAMETER will be returned if the interface is removed.
            // ERROR_NOT_FOUND will be returned if the profile is not found.
            return(CheckResult(nameof(WlanDeleteProfile), result, false));
        }
Esempio n. 17
0
        public static bool Scan(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var result = WlanScan(
                clientHandle,
                interfaceId,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero);

            // ERROR_NDIS_DOT11_POWER_STATE_INVALID will be returned if the interface is turned off.
            return(CheckResult(nameof(WlanScan), result, false));
        }
Esempio n. 18
0
        public static bool SetProfilePosition(SafeClientHandle clientHandle, Guid interfaceId, string profileName, uint position)
        {
            var result = WlanSetProfilePosition(
                clientHandle,
                interfaceId,
                profileName,
                position,
                IntPtr.Zero);

            // ERROR_INVALID_PARAMETER will be returned if the interface is removed.
            // ERROR_NOT_FOUND will be returned if the position of a profile is invalid.
            return(CheckResult(nameof(WlanSetProfilePosition), result, false));
        }
Esempio n. 19
0
        public static bool SetProfile(SafeClientHandle clientHandle, Guid interfaceId, uint profileTypeFlag, string profileXml, string profileSecurity, bool overwrite)
        {
            var result = WlanSetProfile(
                clientHandle,
                interfaceId,
                profileTypeFlag,
                profileXml,
                profileSecurity,
                overwrite,
                IntPtr.Zero,
                out uint pdwReasonCode);

            // ERROR_INVALID_PARAMETER will be returned if the interface is removed.
            // ERROR_ALREADY_EXISTS will be returned if the profile already exists.
            // ERROR_BAD_PROFILE will be returned if the profile xml is not valid.
            // ERROR_NO_MATCH will be returned if the capability specified in the profile is not supported.
            return(CheckResult(nameof(WlanSetProfile), result, false, pdwReasonCode));
        }
Esempio n. 20
0
        public static bool Connect(SafeClientHandle clientHandle, Guid interfaceId, string profileName, DOT11_BSS_TYPE bssType)
        {
            var connectionParameters = new WLAN_CONNECTION_PARAMETERS
            {
                wlanConnectionMode = WLAN_CONNECTION_MODE.wlan_connection_mode_profile,
                strProfile         = profileName,
                dot11BssType       = bssType,
                dwFlags            = 0U
            };

            var result = WlanConnect(
                clientHandle,
                interfaceId,
                ref connectionParameters,
                IntPtr.Zero);

            // ERROR_NOT_FOUND will be returned if the interface is removed.
            return(CheckResult(nameof(WlanConnect), result, false));
        }
Esempio n. 21
0
        public static string GetProfile(SafeClientHandle clientHandle, Guid interfaceId, string profileName, out uint profileTypeFlag)
        {
            uint flags  = 0U;
            var  result = WlanGetProfile(
                clientHandle,
                interfaceId,
                profileName,
                IntPtr.Zero,
                out string profileXml,
                ref flags,
                out uint grantedAccess);

            profileTypeFlag = flags;

            // ERROR_NOT_FOUND will be returned if the profile is not found.
            return(CheckResult(nameof(WlanGetProfile), result, false)
                                ? profileXml
                                : null); // To be used
        }
Esempio n. 22
0
        public static IEnumerable <WLAN_INTERFACE_INFO> GetInterfaceInfoList(SafeClientHandle clientHandle)
        {
            var interfaceList = IntPtr.Zero;

            try
            {
                var result = WlanEnumInterfaces(
                    clientHandle,
                    IntPtr.Zero,
                    out interfaceList);

                return(CheckResult(nameof(WlanEnumInterfaces), result, true)
                                        ? new WLAN_INTERFACE_INFO_LIST(interfaceList).InterfaceInfo
                                        : null); // Not to be used
            }
            finally
            {
                if (interfaceList != IntPtr.Zero)
                {
                    WlanFreeMemory(interfaceList);
                }
            }
        }
Esempio n. 23
0
        private static bool SetInterfaceInt(SafeClientHandle clientHandle, Guid interfaceId, WLAN_INTF_OPCODE wlanIntfOpcode, int value)
        {
            var setData = IntPtr.Zero;

            try
            {
                setData = Marshal.AllocHGlobal(sizeof(int));
                Marshal.WriteInt32(setData, value);

                var result = WlanSetInterface(
                    clientHandle,
                    interfaceId,
                    wlanIntfOpcode,
                    sizeof(int),
                    setData,
                    IntPtr.Zero);

                return(CheckResult(nameof(WlanSetInterface), result, false));
            }
            finally
            {
                Marshal.FreeHGlobal(setData);
            }
        }
Esempio n. 24
0
        public static WLAN_INTERFACE_CAPABILITY GetInterfaceCapability(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var capability = IntPtr.Zero;

            try
            {
                var result = WlanGetInterfaceCapability(
                    clientHandle,
                    interfaceId,
                    IntPtr.Zero,
                    out capability);

                return(CheckResult(nameof(WlanGetInterfaceCapability), result, false)
                                        ? Marshal.PtrToStructure <WLAN_INTERFACE_CAPABILITY>(capability)
                                        : default(WLAN_INTERFACE_CAPABILITY));
            }
            finally
            {
                if (capability != IntPtr.Zero)
                {
                    WlanFreeMemory(capability);
                }
            }
        }
Esempio n. 25
0
        public static IEnumerable <WLAN_PROFILE_INFO> GetProfileInfoList(SafeClientHandle clientHandle, Guid interfaceId)
        {
            var profileList = IntPtr.Zero;

            try
            {
                var result = WlanGetProfileList(
                    clientHandle,
                    interfaceId,
                    IntPtr.Zero,
                    out profileList);

                return(CheckResult(nameof(WlanGetProfileList), result, false)
                                        ? new WLAN_PROFILE_INFO_LIST(profileList).ProfileInfo
                                        : new WLAN_PROFILE_INFO[0]);
            }
            finally
            {
                if (profileList != IntPtr.Zero)
                {
                    WlanFreeMemory(profileList);
                }
            }
        }
Esempio n. 26
0
 public static extern uint WlanEnumInterfaces(
     SafeClientHandle hClientHandle,
     IntPtr pReserved,
     out IntPtr ppInterfaceList);             // Pointer to WLAN_INTERFACE_INFO_LIST
Esempio n. 27
0
 public static extern uint WlanGetInterfaceCapability(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     IntPtr pReserved,
     out IntPtr ppCapability);             // Pointer to WLAN_INTERFACE_CAPABILITY
Esempio n. 28
0
 public static extern uint WlanScan(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     IntPtr pDot11Ssid,
     IntPtr pIeData,
     IntPtr pReserved);
Esempio n. 29
0
 public static extern uint WlanGetAvailableNetworkList(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     uint dwFlags,
     IntPtr pReserved,
     out IntPtr ppAvailableNetworkList);             // Pointer to WLAN_AVAILABLE_NETWORK_LIST
Esempio n. 30
0
 public static extern uint WlanGetProfileList(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     IntPtr pReserved,
     out IntPtr ppProfileList);             // Pointer to WLAN_PROFILE_INFO_LIST