//[DebuggerStepThrough]
    private IFROW_HELPER PrivToPub(MIB_IFROW pri)
    {
        IFROW_HELPER ifhelp = new IFROW_HELPER();

        ifhelp.Name            = pri.wszName.Trim();
        ifhelp.Index           = Convert.ToInt32(pri.dwIndex);
        ifhelp.Type            = Convert.ToInt32(pri.dwType);
        ifhelp.Mtu             = Convert.ToInt32(pri.dwMtu);
        ifhelp.Speed           = Convert.ToInt32(pri.dwSpeed);
        ifhelp.PhysAddrLen     = Convert.ToInt32(pri.dwPhysAddrLen);
        ifhelp.PhysAddr        = Encoding.ASCII.GetString(pri.bPhysAddr);
        ifhelp.AdminStatus     = Convert.ToInt32(pri.dwAdminStatus);
        ifhelp.OperStatus      = Convert.ToInt32(pri.dwOperStatus);
        ifhelp.LastChange      = Convert.ToInt32(pri.dwLastChange);
        ifhelp.InOctets        = pri.dwInOctets; //Convert.ToInt32(pri.dwInOctets);
        ifhelp.InUcastPkts     = Convert.ToInt32(pri.dwInUcastPkts);
        ifhelp.InNUcastPkts    = Convert.ToInt32(pri.dwInNUcastPkts);
        ifhelp.InDiscards      = Convert.ToInt32(pri.dwInDiscards);
        ifhelp.InErrors        = Convert.ToInt32(pri.dwInErrors);
        ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos);
        ifhelp.OutOctets       = Convert.ToInt32(pri.dwOutOctets);
        ifhelp.OutUcastPkts    = Convert.ToInt32(pri.dwOutUcastPkts);
        ifhelp.OutNUcastPkts   = Convert.ToInt32(pri.dwOutNUcastPkts);
        ifhelp.OutDiscards     = Convert.ToInt32(pri.dwOutDiscards);
        ifhelp.OutErrors       = Convert.ToInt32(pri.dwOutErrors);
        ifhelp.OutQLen         = Convert.ToInt32(pri.dwOutQLen);
        ifhelp.Description     = Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen));
        ifhelp.InMegs          = this.ToMegs((long)ifhelp.InOctets);
        ifhelp.OutMegs         = this.ToMegs((long)ifhelp.OutOctets);
        return(ifhelp);
    }
示例#2
0
            public static void GetIfEntry(int index, out MIB_IFROW info)
            {
                info         = new MIB_IFROW();
                info.dwIndex = index;

                long   structSize = Marshal.SizeOf(typeof(MIB_IFROW));
                IntPtr pIfRow     = Marshal.AllocHGlobal(new IntPtr(structSize));

                Marshal.StructureToPtr(info, pIfRow, false);

                try
                {
                    int ret = GetIfEntry(pIfRow);

                    if (ret != 0)
                    {
                        throw new System.ComponentModel.Win32Exception(ret);
                    }

                    info = (MIB_IFROW)Marshal.PtrToStructure(pIfRow, typeof(MIB_IFROW));
                }
                finally
                {
                    Marshal.FreeHGlobal(pIfRow);
                }
            }
示例#3
0
    private IFROW_HELPER PrivToPub(MIB_IFROW pri)
    {
        IFROW_HELPER ifhelp = new IFROW_HELPER();

        ifhelp.Name  = pri.wszName.Trim();
        ifhelp.Index = Convert.ToInt32(pri.dwIndex);
        ifhelp.Type  = Convert.ToInt32(pri.dwType);
        ifhelp.Mtu   = Convert.ToInt32(pri.dwMtu);
        ifhelp.Speed = Convert.ToInt64(pri.dwSpeed);
        // Changed from ToInt32 to ToInt64 for VISTA
        ifhelp.PhysAddrLen = Convert.ToInt32(pri.dwPhysAddrLen);
        ifhelp.PhysAddr    = MAC2String(pri.bPhysAddr);
        ifhelp.AdminStatus = Convert.ToInt32(pri.dwAdminStatus);
        ifhelp.OperStatus  = Convert.ToInt32(pri.dwOperStatus);
        ifhelp.LastChange  = Convert.ToInt64(pri.dwLastChange);
        // Changed from ToInt32 to ToInt64 to make it work and to uncomment
        ifhelp.InOctets        = Convert.ToInt32(pri.dwInOctets);
        ifhelp.InUcastPkts     = Convert.ToInt32(pri.dwInUcastPkts);
        ifhelp.InNUcastPkts    = Convert.ToInt32(pri.dwInNUcastPkts);
        ifhelp.InDiscards      = Convert.ToInt32(pri.dwInDiscards);
        ifhelp.InErrors        = Convert.ToInt32(pri.dwInErrors);
        ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos);
        ifhelp.OutOctets       = Convert.ToInt32(pri.dwOutOctets);
        ifhelp.OutUcastPkts    = Convert.ToInt32(pri.dwOutUcastPkts);
        ifhelp.OutNUcastPkts   = Convert.ToInt32(pri.dwOutNUcastPkts);
        ifhelp.OutDiscards     = Convert.ToInt32(pri.dwOutDiscards);
        ifhelp.OutErrors       = Convert.ToInt32(pri.dwOutErrors);
        ifhelp.OutQLen         = Convert.ToInt32(pri.dwOutQLen);
        ifhelp.Description     = System.Text.Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen));
        ifhelp.InMegs          = ToMegs(ifhelp.InOctets);
        ifhelp.OutMegs         = ToMegs(ifhelp.OutOctets);

        return(ifhelp);
    }
    public clsNetworkStats(bool IgnoreLoopBack)
    {
        int       lRetSize = 0;
        MIB_IFROW ifrow    = new MIB_IFROW();

        byte[] buff = new byte[1];
        byte   val  = 0;
        long   ret  = GetIfTable(ref val, ref lRetSize, 0);

        buff = new byte[lRetSize];
        ret  = GetIfTable(ref buff[0], ref lRetSize, 0);
        int lRows = buff[0];

        this.m_Adapters = new ArrayList(lRows);
        byte len = (byte)lRows;

        for (byte i = 1; i <= len; i++)
        {
            ifrow         = new MIB_IFROW();
            ifrow.dwIndex = Convert.ToUInt32(i);
            ret           = GetIfEntry(ref ifrow);
            IFROW_HELPER ifhelp = this.PrivToPub(ifrow);
            if (IgnoreLoopBack)
            {
                if (ifhelp.Description.IndexOf("Loopback") < 0)
                {
                    this.m_Adapters.Add(ifhelp);
                }
            }
            else
            {
                this.m_Adapters.Add(ifhelp);
            }
        }
    }
示例#5
0
    /// <summary>
    /// New - Undocumented!
    /// </summary>
    /// <param name="IgnoreLoopBack"></param>
    /// <remarks></remarks>
    public clsNetworkStats([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute(true)]      // ERROR: Optional parameters aren't supported in C#
                           bool IgnoreLoopBack)
    {
        long        ret       = 0;
        MIB_IFROW   ifrow     = new MIB_IFROW();
        MIB_IFTABLE IfTable   = new MIB_IFTABLE();
        int         tablesize = 0;
        IntPtr      iBuf      = IntPtr.Zero;

        //get tablesize
        ret = GetIfTable(iBuf, ref tablesize, true);

        //resize buffer on tablesize
        iBuf = Marshal.AllocHGlobal(tablesize);
        //load buffer
        ret = GetIfTable(iBuf, ref tablesize, true);

        //marshar from buffer into MIB_IFTABLE structure
        IfTable = (MIB_IFTABLE)Marshal.PtrToStructure(iBuf, typeof(MIB_IFTABLE));

        //initialize adapter list
        m_Adapters = new ArrayList();

        int noInterfaces = Convert.ToInt32(IfTable.dwNumEntries);
        //NIC Interfaces number
        int IFROWSize = Marshal.SizeOf(ifrow);

        MIB_IFROW[] mrows = new MIB_IFROW[(int)noInterfaces + 1];

        byte[] mDest = null;
        mDest = new byte[IFROWSize * noInterfaces + 1];

        for (byte i = 1; i <= Convert.ToByte(noInterfaces); i++)
        {
            mrows[i - 1] = (MIB_IFROW)Marshal.PtrToStructure(new IntPtr(iBuf.ToInt32() + 4 + (i - 1) * IFROWSize), typeof(MIB_IFROW));
            IFROW_HELPER ifhelp = PrivToPub(mrows[i - 1]);
            if (IgnoreLoopBack == true)
            {
                if (ifhelp.Description.IndexOf("Loopback") < 0)
                {
                    m_Adapters.Add(ifhelp);
                }
            }
            else
            {
                m_Adapters.Add(ifhelp);
            }
        }
        Marshal.FreeHGlobal(iBuf);
    }
示例#6
0
 public void GetIfTableTest()
 {
     Assert.That(() =>
     {
         var t = GetIfTable();
         Assert.That(t.dwNumEntries, Is.GreaterThan(0));
         foreach (var r in t)
         {
             var r2 = new MIB_IFROW(r.dwIndex);
             Assert.That(GetIfEntry(ref r2), Is.Zero);
             Assert.That(r.wszName, Is.EqualTo(r2.wszName));
             Assert.That(SetIfEntry(r2), Is.Zero);
         }
     }, Throws.Nothing);
 }
示例#7
0
        /// <summary>
        /// Get NetInfo Class
        /// </summary>
        /// <param name="row">MIB_IFROW Class</param>
        /// <returns>NetInfo Class</returns>
        private static NetInfo GetNetInfo(MIB_IFROW row)
        {
            NetInfo ninfo = new NetInfo();

            for (int i = 0; i < row.dwPhysAddrLen; i++)
            {
                ninfo.PhysAddr += row.bPhysAddr[i].ToString("X2") + ":";
            }

            if (!string.IsNullOrEmpty(ninfo.PhysAddr))
            {
                ninfo.PhysAddr = ninfo.PhysAddr.TrimEnd(':');
            }
            ninfo.InOctets  = row.dwInOctets;
            ninfo.OutOctets = row.dwOutOctets;
            return(ninfo);
        }
示例#8
0
            public AdaptersTable()
            {
                IntPtr num1    = IntPtr.Zero;
                int    pdwSize = 0;
                int    num2    = 0;

                num2 = Win32Native.GetIfTable(IntPtr.Zero, ref pdwSize, true);
                NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                try
                {
                    num1 = Marshal.AllocHGlobal(pdwSize);
                    if (Win32Native.GetIfTable(num1, ref pdwSize, true) != 0)
                    {
                        return;
                    }
                    int    num3 = Marshal.ReadInt32(num1);
                    IntPtr ptr  = new IntPtr(num1.ToInt32() + 4);
                    for (int index = 0; index < num3; ++index)
                    {
                        MIB_IFROW structure = (MIB_IFROW)Marshal.PtrToStructure(ptr, typeof(MIB_IFROW));
                        MIB_IFROW pIfRow    = new MIB_IFROW();
                        pIfRow.dwIndex = structure.dwIndex;
                        if (Win32Native.GetIfEntry(ref pIfRow) == 0)
                        {
                            string str = Encoding.ASCII.GetString(structure.bDescr, 0, pIfRow.dwDescrLen - 1);
                            foreach (NetworkInterface networkInterface in networkInterfaces)
                            {
                                if (networkInterface.Description == str)
                                {
                                    this._adapters.Add(structure.dwIndex, networkInterface);
                                    break;
                                }
                            }
                        }
                        ptr = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(typeof(MIB_IFROW)));
                    }
                }
                catch (Exception)
                {
                    // 跳过
                }
                finally
                {
                    Marshal.FreeHGlobal(num1);
                }
            }
示例#9
0
 public extern static int GetIfEntry(ref MIB_IFROW pIfRow);
 internal static extern int GetIfEntry(ref MIB_IFROW pIfRow);
示例#11
0
 private static extern Int32 GetIfEntry(ref MIB_IFROW pIfRow);
 private static extern int GetIfEntry(ref MIB_IFROW pIfRow);