Example #1
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 uint dataSize,
                    out queryData,
                    out WLAN_OPCODE_VALUE_TYPE opcodeValueType);

                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);
                }
            }
        }
        public static string GetProfile(SafeClientHandle clientHandle, Guid interfaceId, string profileName, out ProfileType profileType)
        {
            var profileXml = IntPtr.Zero;

            try
            {
                uint flags = 0U;
                uint grantedAccess;
                var  result = WlanGetProfile(
                    clientHandle,
                    interfaceId,
                    profileName,
                    IntPtr.Zero,
                    out profileXml,
                    ref flags,
                    out grantedAccess);

                profileType = Enum.IsDefined(typeof(ProfileType), (int)flags)
                                        ? (ProfileType)(int)flags
                                        : default(ProfileType);

                // ERROR_NOT_FOUND will be returned if the profile is not found.
                return(CheckResult(nameof(WlanGetProfile), result, false)
                                        ? Marshal.PtrToStringUni(profileXml)
                                        : null); // To be used
            }
            finally
            {
                if (profileXml != IntPtr.Zero)
                {
                    WlanFreeMemory(profileXml);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Gets a parameter of the interface whose data type is <see cref="int"/>.
        ///
        /// Possible Win32 errors:
        /// ERROR_ACCESS_DENIED: The caller does not have sufficient permissions to perform the requested operation.
        /// ERROR_INVALID PARAMETER: hClientHandle is NULL or invalid, pInterfaceGuid is NULL, pReserved is not NULL, ppData is NULL, or pdwDataSize is NULL.
        /// ERROR_INVALID_HANDLE: The handle hClientHandle was not found in the handle table.
        /// ERROR_INVALID_STATE: OpCode is set to wlan_intf_opcode_current_connection and the client is not currently connected to a network.
        /// ERROR_NOT_ENOUGH_MEMORY: Failed to allocate memory for the query results.
        /// RPC_STATUS: Various error codes.
        /// </summary>
        /// <param name="clientHandle">The client's session handle, obtained by a previous call to the WlanOpenHandle function.</param>
        /// <param name="interfaceId">The GUID of the interface to be queried.</param>
        /// <param name="wlanIntfOpcode">A WLAN_INTF_OPCODE value that specifies the parameter to be queried.</param>
        /// <returns>The integer value.</returns>
        public 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 uint dataSize,
                    out queryData,
                    out WLAN_OPCODE_VALUE_TYPE opcodeValueType);

                // ERROR_INVALID_STATE will be returned if the client is not connected to a network.
                return(CheckResult(nameof(GetInterfaceInt), result, false)
                                    ? Marshal.ReadInt32(queryData)
                                    : -1);
            }
            finally
            {
                if (queryData != IntPtr.Zero)
                {
                    WlanFreeMemory(queryData);
                }
            }
        }
Example #4
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 uint dataSize,
                    out queryData,
                    out WLAN_OPCODE_VALUE_TYPE opcodeValueType);

                // 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);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Retrieves a list of the basic service set (BSS) entries of the wireless network or networks from specified the SSID on a given wireless LAN interface.
        /// </summary>
        /// <param name="clientHandle"></param>
        /// <param name="interfaceId"></param>
        /// <param name="ssid"></param>
        /// <param name="bssType">dot11_BSS_type_infrastructure or dot11_BSS_type_independent</param>
        /// <returns></returns>
        public static IEnumerable <WLAN_BSS_ENTRY> GetNetworkBssEntryList(SafeClientHandle clientHandle, Guid interfaceId, DOT11_SSID ssid, DOT11_BSS_TYPE bssType)
        {
            IntPtr ssidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ssid));

            Marshal.StructureToPtr(ssid, ssidPtr, false);
            var wlanBssList = IntPtr.Zero;

            try
            {
                var result = WlanGetNetworkBssList(
                    clientHandle,
                    interfaceId,
                    ssidPtr,
                    bssType,
                    true,                 //must true
                    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);
                }
            }
        }
Example #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);
                }
            }
        }
Example #7
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);
                }
            }
        }
Example #8
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);
                }
            }
        }
Example #9
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);
            }
        }
Example #10
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);
                }
            }
        }
 public static extern uint WlanSetProfileEapXmlUserData(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct), In] Guid pInterfaceGuid,
     [MarshalAs(UnmanagedType.LPWStr)] string strProfileName,
     uint dwFlags,
     [MarshalAs(UnmanagedType.LPWStr)] string strEapXmlUserData,
     IntPtr pReserved);
Example #12
0
 public static extern uint WlanSetInterface(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct)] Guid pInterfaceGuid,
     WLAN_INTF_OPCODE OpCode,
     uint dwDataSize,
     IntPtr pData,             // Pointer to WLAN_PHY_RADIO_STATE
     IntPtr pReserved);
Example #13
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);
Example #14
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);
        }
Example #15
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 WLAN_CONNECTION_ATTRIBUTES, WLAN_RADIO_STATE
     out WLAN_OPCODE_VALUE_TYPE pWlanOpcodeValueType);
Example #16
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
Example #17
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);
 public static extern uint WlanQueryInterface(
     SafeClientHandle hClientHandle,
     [MarshalAs(UnmanagedType.LPStruct), In] Guid pInterfaceGuid,
     WLAN_INTF_OPCODE OpCode,
     IntPtr pReserved,
     out uint pdwDataSize,
     out IntPtr ppData,             // Pointer to queried data
     IntPtr pWlanOpcodeValueType);
Example #19
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
Example #20
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));
        }
Example #21
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));
        }
Example #22
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));
        }
Example #23
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));
        }
Example #24
0
        public static void RegisterNotification(SafeClientHandle clientHandle, uint notificationSource, Action <IntPtr, IntPtr> callback)
        {
            // Storing a delegate in class field is necessary to prevent garbage collector from collecting it
            // before the delegate is called. Otherwise, CallbackOnCollectedDelegate may occur.
            _notificationCallback = new WLAN_NOTIFICATION_CALLBACK(callback);

            var result = WlanRegisterNotification(clientHandle,
                                                  notificationSource,
                                                  false,
                                                  _notificationCallback,
                                                  IntPtr.Zero,
                                                  IntPtr.Zero,
                                                  0);

            CheckResult(nameof(WlanRegisterNotification), result, true);
        }
Example #25
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));
        }
Example #26
0
        public static string GetProfileUnProtected(SafeClientHandle clientHandle, Guid interfaceId, string profileName, out uint profileTypeFlag)
        {
            uint flags  = 4U; //GetPlaintextKey
            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
        }
Example #27
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));
        }
Example #28
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);
        public static bool SetProfileEapXmlUserData(SafeClientHandle clientHandle, Guid interfaceId, string profileName, SetEapUserDataMode eapUserDataModeFlag, string userDataXML)
        {
            var result = WlanSetProfileEapXmlUserData(
                clientHandle,
                interfaceId,
                profileName,
                eapUserDataModeFlag,
                userDataXML,
                IntPtr.Zero);

            //ERROR_ACCESS_DENIED Access is denied.
            //ERROR_BAD_PROFILE The network connection profile is corrupted.
            //ERROR_INVALID_PARAMETER A parameter is incorrect.
            //ERROR_INVALID_HANDLE A handle is invalid.
            //ERROR_NOT_ENOUGH_MEMORY Not enough storage is available to process this command.
            //ERROR_NOT_SUPPORTED The request is not supported.
            //ERROR_SERVICE_NOT_ACTIVE The service has not been started.
            //RPC_STATUS Various error codes.
            return(CheckResult(nameof(WlanSetProfileEapXmlUserData), result, false));
        }
Example #30
0
        /// <summary>
        /// Sets a parameter of the interface whose data type is <see cref="int"/>.
        ///
        /// Possible Win32 errors:
        /// ERROR_ACCESS_DENIED: The caller does not have sufficient permissions to perform the requested operation.
        /// ERROR_GEN_FAILURE: The parameter specified by OpCode is not supported by the driver or NIC.
        /// ERROR_INVALID_HANDLE: The handle hClientHandle was not found in the handle table.
        /// ERROR_INVALID_PARAMETER: One parameter is likely NULL
        /// RPC_STATUS: Various return codes to indicate errors occurred when connecting.
        /// </summary>
        /// <param name="clientHandle">The client's session handle, obtained by a previous call to the WlanOpenHandle function.</param>
        /// <param name="interfaceId">The GUID of the interface to be configured.</param>
        /// <param name="wlanIntfOpcode">A WLAN_INTF_OPCODE value that specifies the parameter to be set.</param>
        /// <param name="value">The value to set.</param>
        public static void SetIntesrfaceInt(SafeClientHandle clientHandle, Guid interfaceId,
                                            WLAN_INTF_OPCODE wlanIntfOpcode, int value)
        {
            IntPtr dataPtr = Marshal.AllocHGlobal(sizeof(int));

            Marshal.WriteInt32(dataPtr, value);
            try
            {
                var result = WlanSetInterface(
                    clientHandle,
                    interfaceId,
                    wlanIntfOpcode,
                    sizeof(int),
                    dataPtr,
                    IntPtr.Zero
                    );
            }
            finally
            {
                Marshal.FreeHGlobal(dataPtr);
            }
        }