|
适用vista以上系统。。。
- namespace netMonitor{
- import win.guid
-
- var dll = ..raw.loadDll("iphlpapi")
- getIfTable2 = dll.api("GetIfTable2", "int(ptr pIfTable)")
- freeMibTable = dll.api("FreeMibTable", "int(ptr pIfTable)")
-
- var guid = win.guid
- class MIB_IF_ROW2{
- LONG InterfaceLuid;
- int InterfaceIndex;
- struct InterfaceGuid = guid();
- byte Alias[514];
- byte Description[514];
- int PhysicalAddressLength;
- BYTE PhysicalAddress[32];
- BYTE PermanentPhysicalAddress[32];
- int Mtu;
- int Type;
- int TunnelType;
- int MediaType;
- int PhysicalMediumType;
- int AccessType;
- int DirectionType;
- byte InterfaceAndOperStatusFlags;
- int OperStatus;
- int AdminStatus;
- int MediaConnectState;
- struct NetworkGuid = guid();
- int ConnectionType;
- LONG TransmitLinkSpeed;
- LONG ReceiveLinkSpeed;
- LONG InOctets;
- LONG InUcastPkts;
- LONG InNUcastPkts;
- LONG InDiscards;
- LONG InErrors;
- LONG InUnknownProtos;
- LONG InUcastOctets;
- LONG InMulticastOctets;
- LONG InBroadcastOctets;
- LONG OutOctets;
- LONG OutUcastPkts;
- LONG OutNUcastPkts;
- LONG OutDiscards;
- LONG OutErrors;
- LONG OutUcastOctets;
- LONG OutMulticastOctets;
- LONG OutBroadcastOctets;
- LONG OutQLen;
- }
-
- getInfo = function(){
- var ptr = ..raw.buffer(4)
- var ret = getIfTable2(ptr)
- if(ret) return;
-
- ptr = ..raw.convert(ptr, {ptr p}).p
- ret = ..raw.convert(ptr, {int n}).n
- ret = ..raw.convert(ptr, {
- int NumEntries;
- struct table[]={
- length = ret;
- MIB_IF_ROW2()
- };
- })
- freeMibTable(ptr)
- return ret;
- }
- }
- import win.ui
- import win.timer
- /*DSG{{*/
- var winform = win.form(text="网卡流量监控";right=1004;bottom=591)
- winform.add(
- dtLv={cls="listview";left=2;top=41;right=1001;bottom=563;edge=1;fullRow=1;gridLines=1;z=1}
- )
- /*}}*/
- winform.dtLv.insertColumn("名称", 230)
- winform.dtLv.insertColumn("MAC", 130)
- winform.dtLv.insertColumn("总发送", 100)
- winform.dtLv.insertColumn("总接收", 100)
- winform.dtLv.insertColumn("上行", 100)
- winform.dtLv.insertColumn("下行", 100)
- winform.dtLv.insertColumn("描述", 300)
- winform.dtLv.adjust = function(cx,cy){
- winform.dtLv.fillParent(7/*列序号*/);
- }
- winform.dtLv.setExtended(0x10000/*_LVS_EX_DOUBLEBUFFER*/)
- var agoT = netMonitor.getInfo()
- for(i=1;agoT.NumEntries){
- var u = agoT.table[ i ]
- //**
- var mac = ..string.format("%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",
- u.PhysicalAddress[1],
- u.PhysicalAddress[2],
- u.PhysicalAddress[3],
- u.PhysicalAddress[4],
- u.PhysicalAddress[5],
- u.PhysicalAddress[6]
- )
- var guid = ..string.upper(tostring(u.InterfaceGuid))
- //**/
- winform.dtLv.addItem({..string.fromUtf16(u.Alias);mac;u.OutOctets;u.InOctets;"";"";..string.fromUtf16(u.Description)},i)
- }
- var mR = ..math.round
- var atimer = win.timer(,500)
- atimer.onTimer = function(hwnd,msg,id,tick){
- var nowT = netMonitor.getInfo()
- if(!agoT){
- agoT = ..table.clone(nowT)
- return;
- }
- for(i=1;nowT.NumEntries){
- var a, n = agoT.table[ i ], nowT.table[ i ]
-
- var aTx, aRx = a.OutOctets, a.InOctets
- var nTx, nRx = n.OutOctets, n.InOctets
-
- aTx ?= mR((nTx-aTx)/512, 2) + 'kb/s'
- aRx ?= mR((nRx-aRx)/512, 2) + 'kb/s'
-
- winform.dtLv.setItemText(nTx, i, 3)
- winform.dtLv.setItemText(nRx, i, 4)
- winform.dtLv.setItemText(aTx, i, 5)
- winform.dtLv.setItemText(aRx, i, 6)
-
- a.InOctets = nRx
- a.OutOctets = nTx
- }
- }
- atimer.enable()
- winform.show();
- win.loopMessage()
复制代码 |
|