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;
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值