c# 获取网络流量

public class ip_helper
{
enum Constants {
MAX_INTERFACE_NAME_LEN=256, MAXLEN_PHYSADDR=8,MAXLEN_IFDESCR=256
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MIB_IFROW
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
=(int)Constants.MAX_INTERFACE_NAME_LEN)]
public string wszName;
public uint dwIndex;
public uint dwType;
public uint dwMtu;
public uint dwSpeed;
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=(int)Constants.MAXLEN_PHYSADDR)]
public byte[] bPhysAddr;
public uint dwAdminStatus;
public uint dwOperStatus;
public uint dwLastChange;
public uint dwInOctets;
public uint dwInUcastPkts;
public uint dwInNUcastPkts;
public uint dwInDiscards;
public uint dwInErrors;
public uint dwInUnknownProtos;
public uint dwOutOctets;
public uint dwOutUcastPkts;
public uint dwOutNUcastPkts;
public uint dwOutDiscards;
public uint dwOutErrors;
public uint dwOutQLen;
public uint dwDescrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst
=(int)Constants.MAXLEN_IFDESCR)]
public byte[] bDescr;
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IFTABLE
{
public uint dwNumEntries;
public MIB_IFROW[] table;
}
[DllImport("IPHlpApi.dll")]
public static extern uint GetIfTable(IntPtr pIfTable, ref uint pdwSize, bool
bOrder);
/// <summary>
/// Description of MyClass.
/// </summary>
public ip_helper()
{
}
public int InitGetIfTable()
{
uint size = 0;
GetIfTable(IntPtr.Zero, ref size, false);
IntPtr buf = Marshal.AllocHGlobal((int)size);
GetIfTable(buf, ref size, false);
int numEntries = Marshal.ReadInt32(buf);
int pRows = 4 + (int)buf;
MIB_IFROW[] rows = new MIB_IFROW[(int)numEntries];
for ( int i = 0; i < numEntries; i++ )
{
rows[i] = Marshal.PtrToStructure((IntPtr)pRows, typeof(MIB_IFROW));
pRows += Marshal.SizeOf(typeof(MIB_IFROW));
}
Marshal.FreeHGlobal(buf);
return 1;
}
//
private MIB_IFTABLE m_pfTable;
private ulong m_dwAdapters;
}
}
posted @ 2014-09-13 17:01  IT苦行僧  阅读(889)  评论(0编辑  收藏  举报