C# 监控网络流量 MIB_IF_TABLE2

来源:互联网 发布:汇率换算软件 ios 编辑:程序博客网 时间:2024/05/01 22:39

通过获取 MIB2Interface 实现,不过由于此源码在地址指针上用的很多,不是太建议对指针不熟悉的人观看

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace Demo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        [StructLayout(LayoutKind.Sequential)]        struct MIB_IF_ROW2 // sizeof(1352 + 4)        {            public long InterfaceLuid;            public int InterfaceIndex;            public byte[] GUID;            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0)] // 514            public string Alias;            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0)]            public string Description;            public int PhysicalAddressLength;            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]            public byte[] PhysicalAddress;            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]            public byte[] PermanentPhysicalAddress;            public int Mtu;            public int Type;            public int TunnelType;            public int MediaType;            public int PhysicalMediumType;            public int AccessType;            public int DirectionType;            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]            public byte[] InterfaceAndOperStatusFlags;            public int OperStatus;            public int AdminStatus;            public int MediaConnectState;            public byte[] NetworkGuid;            public int ConnectionType;            public long TransmitLinkSpeed;            public long ReceiveLinkSpeed;            public long InOctets;            public long InUcastPkts;            public long InNUcastPkts;            public long InDiscards;            public long InErrors;            public long InUnknownProtos;            public long InUcastOctets;            public long InMulticastOctets;            public long InBroadcastOctets;            public long OutOctets;            public long OutUcastPkts;            public long OutNUcastPkts;            public long OutDiscards;            public long OutErrors;            public long OutUcastOctets;            public long OutMulticastOctets;            public long OutBroadcastOctets;            public long OutQLen;        }        [StructLayout(LayoutKind.Sequential)]        struct MIB_IF_TABLE2        {            public int NumEntries;            public MIB_IF_ROW2[] Table;        }        [DllImportAttribute("Iphlpapi.dll")]        static extern int GetIfTable2(ref int Table);        [DllImportAttribute("Iphlpapi.dll")]        static extern int FreeMibTable(int Table);        [DllImportAttribute("kernel32.dll", EntryPoint = "RtlMoveMemory")]        static extern int RtlMoveMemory(ref int Destination, int Source, int Length);        [DllImportAttribute("kernel32.dll", EntryPoint = "RtlMoveMemory")]        static extern int RtlMoveMemory(ref long Destination, int Source, int Length);        [DllImportAttribute("kernel32.dll", EntryPoint = "RtlMoveMemory")]        static extern int RtlMoveMemory(int Destination, int Source, int Length);        [DllImportAttribute("kernel32.dll")]        static extern int SetHandleCount(byte[] Bytes);        private void Form1_Load(object sender, EventArgs e)        {            MIB_IF_TABLE2 MIB_IF_TABLE2 = this.GetMIB2Interface();            for (int i = 0; i < MIB_IF_TABLE2.NumEntries; i++) {                ListViewItem Item = listView1.Items.Add(MIB_IF_TABLE2.Table[i].Alias);                Item.SubItems.Add(MIB_IF_TABLE2.Table[i].Mtu.ToString());                Item.SubItems.Add(MIB_IF_TABLE2.Table[i].OutOctets.ToString());                Item.SubItems.Add(MIB_IF_TABLE2.Table[i].InOctets.ToString());                Item.SubItems.Add(MIB_IF_TABLE2.Table[i].Description);            }                }        MIB_IF_TABLE2 GetMIB2Interface()        {            int pTable = 0;            byte[] bin = null;            MIB_IF_TABLE2 MIB_IF_TABLE2 = new MIB_IF_TABLE2();            int ret = GetIfTable2(ref pTable);            if(ret != 0) {                return MIB_IF_TABLE2;            }            int leng = 1352;            RtlMoveMemory(ref MIB_IF_TABLE2.NumEntries, pTable, 4);            MIB_IF_TABLE2.Table = new MIB_IF_ROW2[MIB_IF_TABLE2.NumEntries];            int Address = pTable + 8;            for (int i = 0; i < MIB_IF_TABLE2.NumEntries; i++) {                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InterfaceLuid, Address + (i) * leng, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InterfaceIndex, Address + (i) * leng + 8, 4);                MIB_IF_TABLE2.Table[i].GUID = new byte[16];                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].GUID), Address + (i) * leng + 12, 16);                bin = new byte[514];                RtlMoveMemory(SetHandleCount(bin), Address + (i) * leng + 28, 514);                MIB_IF_TABLE2.Table[i].Alias = Encoding.Unicode.GetString(bin);                bin = new byte[514];                RtlMoveMemory(SetHandleCount(bin), Address + (i) * leng + 542, 514);                MIB_IF_TABLE2.Table[i].Description = Encoding.Unicode.GetString(bin);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].PhysicalAddressLength, Address + (i) * leng + 1056, 4);                MIB_IF_TABLE2.Table[i].PhysicalAddress = new byte[32];                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].PhysicalAddress), Address + (i) * leng + 1060, 32);                MIB_IF_TABLE2.Table[i].PermanentPhysicalAddress = new byte[32];                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].PermanentPhysicalAddress), Address + (i) * leng + 1092, 32);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].Mtu, Address + (i) * leng + 1124, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].Type, Address + (i) * leng + 1128, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].TunnelType, Address + (i) * leng + 1132, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].MediaType, Address + (i) * leng + 1136, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].PhysicalMediumType, Address + (i) * leng + 1140, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].AccessType, Address + (i) * leng + 1144, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].DirectionType, Address + (i) * leng + 1148, 4);                MIB_IF_TABLE2.Table[i].InterfaceAndOperStatusFlags = new byte[8];                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].InterfaceAndOperStatusFlags), Address + (i) * leng + 1152, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OperStatus, Address + (i) * leng + 1160, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].AdminStatus, Address + (i) * leng + 1164, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].MediaConnectState, Address + (i) * leng + 1168, 4);                MIB_IF_TABLE2.Table[i].NetworkGuid = new byte[16];                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].NetworkGuid), Address + (i) * leng + 1172, 16);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].ConnectionType, Address + (i) * leng + 1188, 4);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].TransmitLinkSpeed, Address + (i) * leng + 1192, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].ReceiveLinkSpeed, Address + (i) * leng + 1200, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InOctets, Address + (i) * leng + 1208, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUcastPkts, Address + (i) * leng + 1216, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InNUcastPkts, Address + (i) * leng + 1224, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InDiscards, Address + (i) * leng + 1232, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InErrors, Address + (i) * leng + 1240, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUnknownProtos, Address + (i) * leng + 1248, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUcastOctets, Address + (i) * leng + 1256, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InMulticastOctets, Address + (i) * leng + 1264, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InBroadcastOctets, Address + (i) * leng + 1272, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutOctets, Address + (i) * leng + 1280, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutUcastPkts, Address + (i) * leng + 1288, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutNUcastPkts, Address + (i) * leng + 1296, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutDiscards, Address + (i) * leng + 1304, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutErrors, Address + (i) * leng + 1312, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutUcastOctets, Address + (i) * leng + 1320, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutMulticastOctets, Address + (i) * leng + 1328, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutBroadcastOctets, Address + (i) * leng + 1336, 8);                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutQLen, Address + (i) * leng + 1344, 8);            }            FreeMibTable(pTable);            return MIB_IF_TABLE2;        }        private void timer1_Tick(object sender, EventArgs e)        {            MIB_IF_TABLE2 MIB_IF_TABLE2 = this.GetMIB2Interface();            for (int i = 0; i < MIB_IF_TABLE2.NumEntries; i++) {                listView1.Items[i].SubItems[2].Text = MIB_IF_TABLE2.Table[i].OutOctets.ToString();                listView1.Items[i].SubItems[3].Text = MIB_IF_TABLE2.Table[i].InOctets.ToString();            }        }    }}

 

0 0
原创粉丝点击