Packet Sniffer SDK DLL Edition

HNAdapterConfig wrapper for C#

 Previous Next

File: HNAdapterConfig.cs

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace microOLAP.PSSDK
{
    /// <summary>
    /// Summary description for HNAdapterConfig.
    /// </summary>
    public partial class HNAdapterConfig : Component
    {
        #region DLL exports

        /// <summary>
        /// Returns the network adapter system name.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetAdapterNameW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetAdapterName(IntPtr hCfg);

        /// <summary>
        /// Returns the network adapter accessibility status.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetAccessibleState")]
        private static extern bool AdpCfgGetAccessibleState(IntPtr hCfg);

        /// <summary>
        /// Returns the network adapter description.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetAdapterDescriptionW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetAdapterDescription(IntPtr hCfg);

        /// <summary>
        /// AdpCfgGetAdapterType - Returns the network adapter type.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetAdapterType")]
        private static extern int AdpCfgGetAdapterType(IntPtr hCfg);

        /// <summary>
        /// Returns the network adapter packet maximum size.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetMaxPacketSize")]
        private static extern uint AdpCfgGetMaxPacketSize(IntPtr hCfg);

        /// <summary>
        /// Returns the MAC address size.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetMACAddrSize")]
        private static extern uint AdpCfgGetMACAddrSize(IntPtr hCfg);

        /// <summary>
        /// AdpCfgGetMACAddress - Returns the MAC address of the network adapter.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="MacAddress"></param>
        /// <param name="BufferSize"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetMACAddress")]
        private static extern int AdpCfgGetMACAddress(IntPtr hCfg, byte[] MacAddress, uint BufferSize);

        /// <summary>
        /// Returns the MAC header size.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetMACHeaderSize")]
        private static extern uint AdpCfgGetMACHeaderSize(IntPtr hCfg);

        /// <summary>
        /// Returns the status of IP zero broadcast usage.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetZeroBroadcastState")]
        private static extern bool AdpCfgGetZeroBroadcastState(IntPtr hCfg);

        /// <summary>
        /// Returns the status of the DHCP using.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetDhcpState")]
        private static extern bool AdpCfgGetDhcpState(IntPtr hCfg);

        /// <summary>
        /// Returns the number of IP addresses, assigned to the network adapter card.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpCount")]
        private static extern int AdpCfgGetIpCount(IntPtr hCfg);

        /// <summary>
        /// Returns one of the IP addresses, assigned to the network adapter.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="Index"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetIp(IntPtr hCfg, int Index);

        /// <summary>
        /// Returns the subnet mask that matches to the corresponding IP address index.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="Index"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetSubnetMaskW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetSubnetMask(IntPtr hCfg, int Index);

        /// <summary>
        /// Returns the number of the IP addresses of DHCP servers set on the system.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpDhcpCount")]
        private static extern int AdpCfgGetIpDhcpCount(IntPtr hCfg);

        /// <summary>
        /// Returns one of the IP addresses of DHCP servers set on the system.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="Index"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpDhcpW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetIpDhcp(IntPtr hCfg, int Index);

        /// <summary>
        /// Returns the number of the gateways IP addresses.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpGatewayCount")]
        private static extern int AdpCfgGetIpGatewayCount(IntPtr hCfg);

        /// <summary>
        /// Returns one of the IP addresses of the gateways set on the system.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="Index"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpGatewayW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetIpGateway(IntPtr hCfg, int Index);

        /// <summary>
        /// Returns the number of the IP addresses of DNS servers set on the system.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpDnsCount")]
        private static extern int AdpCfgGetIpDnsCount(IntPtr hCfg);

        /// <summary>
        /// Returns one of the IP addresses of the DNS servers set on the system.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="Index"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgGetIpDnsW", CharSet = CharSet.Unicode)]
        private static extern IntPtr AdpCfgGetIpDns(IntPtr hCfg, int Index);

        /// <summary>
        /// Updates the network adapter configuration.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <param name="ChangeType"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgUpdate")]
        private static extern int AdpCfgUpdate(IntPtr hCfg, int ChangeType);

        /// <summary>
        /// Return the value of the flag which indicates that the network adapter actually is wireless (802.11) device.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "AdpCfgIsWireless")]
        private static extern bool AdpCfgIsWireless(IntPtr hCfg);

        #endregion

        /// <summary>
        /// Handle of the HNAdapterConfig object.
        /// </summary>
        private IntPtr hCfg = IntPtr.Zero;

        /// <summary>
        /// Constructor of the HNAdapterConfig object.
        /// </summary>
        public HNAdapterConfig()
        {
            InitializeComponent();
        }

        #region HNAdapterConfig properties

        /// <summary>
        /// Checks the correctness of the HNAdapterConfig object handle settings.
        /// </summary>
        [Browsable(false)]
        public bool IsValid
        {
            get
            {
                return (hCfg != IntPtr.Zero);
            }
        }

        /// <summary>
        /// Sets/returns a handle of the network adapter configuration.
        /// </summary>
        [Browsable(false)]
        public IntPtr Handle
        {
            get
            {
                return hCfg;
            }
            set
            {
                hCfg = value;
            }
        }

        /// <summary>
        /// Returns the network adapter system name.
        /// </summary>
        [Browsable(false)]
        public string AdapterName
        {
            get
            {
                return Marshal.PtrToStringUni(AdpCfgGetAdapterName(hCfg));
            }
        }

        /// <summary>
        /// Returns the network adapter accessibility status.
        /// </summary>
        [Browsable(false)]
        public bool AccessibleState
        {
            get
            {
                return AdpCfgGetAccessibleState(hCfg);
            }
        }

        /// <summary>
        /// Returns the network adapter description.
        /// </summary>
        [Browsable(false)]
        public string AdapterDescription
        {
            get
            {
                return Marshal.PtrToStringUni(AdpCfgGetAdapterDescription(hCfg));
            }
        }

        /// <summary>
        /// Returns the network adapter type.
        /// </summary>
        [Browsable(false)]
        public HNNetAdapterType AdapterType
        {
            get
            {
                return (HNNetAdapterType)AdpCfgGetAdapterType(hCfg);
            }
        }

        /// <summary>
        /// Returns the network adapter packet maximum size.
        /// </summary>
        [Browsable(false)]
        public uint MaxPacketSize
        {
            get
            {
                return AdpCfgGetMaxPacketSize(hCfg);
            }
        }

        /// <summary>
        /// Returns the MAC address size.
        /// </summary>
        [Browsable(false)]
        public uint MACAddrSize
        {
            get
            {
                return AdpCfgGetMACAddrSize(hCfg);
            }
        }

        /// <summary>
        /// Returns the MAC address of the network adapter.
        /// </summary>
        /// <param name="MACAddr"></param>
        /// <param name="BuffSize"></param>
        /// <returns></returns>
        public PSSDKRES Get_MACAddress(byte[] MACAddr, uint BuffSize)
        {
            return (PSSDKRES)AdpCfgGetMACAddress(hCfg, MACAddr, BuffSize);
        }

        /// <summary>
        /// Returns the MAC header size.
        /// </summary>
        [Browsable(false)]
        public uint MACHeaderSize
        {
            get
            {
                return AdpCfgGetMACHeaderSize(hCfg);
            }
        }

        /// <summary>
        /// Returns the status of IP zero broadcast usage.
        /// </summary>
        [Browsable(false)]
        public bool ZeroBroadcastState
        {
            get
            {
                return AdpCfgGetZeroBroadcastState(hCfg);
            }
        }

        /// <summary>
        /// Returns the status of the DHCP using.
        /// </summary>
        [Browsable(false)]
        public bool DhcpState
        {
            get
            {
                return AdpCfgGetDhcpState(hCfg);
            }
        }

        /// <summary>
        /// Returns the number of IP addresses, assigned to the network adapter card.
        /// </summary>
        [Browsable(false)]
        public int IpCount
        {
            get
            {
                return AdpCfgGetIpCount(hCfg);
            }
        }

        /// <summary>
        /// Returns the number of the IP addresses of DHCP servers set on the system.
        /// </summary>
        [Browsable(false)]
        public int IpDhcpCount
        {
            get
            {
                return AdpCfgGetIpDhcpCount(hCfg);
            }
        }

        /// <summary>
        /// Returns the number of the gateways IP addresses.
        /// </summary>
        [Browsable(false)]
        public int IpGatewayCount
        {
            get
            {
                return AdpCfgGetIpGatewayCount(hCfg);
            }
        }

        /// <summary>
        /// Returns the number of the IP addresses of DNS servers set on the system.
        /// </summary>
        [Browsable(false)]
        public int IpDnsCount
        {
            get
            {
                return AdpCfgGetIpDnsCount(hCfg);
            }
        }

        /// <summary>
        /// Return the value of the flag which indicates that the network adapter actually is wireless (802.11) device.
        /// </summary>
        /// <param name="hCfg"></param>
        /// <returns></returns>
        [Browsable(false)]
        public bool IsWireless
        {
            get
            {
                return AdpCfgIsWireless(hCfg);
            }
        }

        #endregion

        #region HNAdapterConfig methods

        /// <summary>
        /// Returns one of the IP addresses, assigned to the network adapter.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public string Get_Ip(int Index)
        {
            return Marshal.PtrToStringUni(AdpCfgGetIp(hCfg, Index));
        }

        /// <summary>
        /// Returns the subnet mask that matches to the corresponding IP address index.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public string Get_SubnetMask(int Index)
        {
            return Marshal.PtrToStringUni(AdpCfgGetSubnetMask(hCfg, Index));
        }

        /// <summary>
        /// Returns one of the IP addresses of DHCP servers set on the system.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public string Get_IpDhcp(int Index)
        {
            return Marshal.PtrToStringUni(AdpCfgGetIpDhcp(hCfg, Index));
        }

        /// <summary>
        /// Returns one of the IP addresses of the gateways set on the system.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public string Get_IpGateway(int Index)
        {
            return Marshal.PtrToStringUni(AdpCfgGetIpGateway(hCfg, Index));
        }

        /// <summary>
        /// Returns one of the IP addresses of the DNS servers set on the system.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public string Get_IpDns(int Index)
        {
            return Marshal.PtrToStringUni(AdpCfgGetIpDns(hCfg, Index));
        }

        /// <summary>
        /// Updates the network adapter configuration.
        /// </summary>
        /// <param name="ChangeType"></param>
        /// <returns></returns>
        public PSSDKRES Update(int ChangeType)
        {
            return (PSSDKRES)AdpCfgUpdate(hCfg, ChangeType);
        }

        #endregion
    }
}
See also:HNAdapterConfig, All wrappers in one ZIP archive