| 发表于:2008-01-14 11:15:024楼 得分:0 |
unit adputils; interface uses windows; const max_interface_name_len = 256; maxlen_physaddr = 8; maxlen_ifdescr = 256; mib_if_type_other = 1; mib_if_type_ethernet = 6; mib_if_type_tokenring = 9; mib_if_type_fddi = 15; mib_if_type_ppp = 23; mib_if_type_loopback = 24; mib_if_type_slip = 28; mib_if_admin_status_up = 1; mib_if_admin_status_down = 2; mib_if_admin_status_testing = 3; mib_if_oper_status_non_operational = 0; mib_if_oper_status_unreachable = 1; mib_if_oper_status_disconnected = 2; mib_if_oper_status_connecting = 3; mib_if_oper_status_connected = 4; mib_if_oper_status_operational = 5; type mib_physaddr = array[0..maxlen_physaddr - 1] of byte; mib_ifdescr = array[0..maxlen_ifdescr - 1] of char; pmib_ifrow = ^mib_ifrow; mib_ifrow = packed record wszname: array[0..max_interface_name_len - 1] of wchar; dwindex, dwtype, dwmtu, dwspeed, dwphysaddrlen: dword; bphysaddr: mib_physaddr; dwadminstatus, dwoperstatus, dwlastchange, dwinoctets, dwinucastpkts, dwinnucastpkts, dwindiscards, dwinerrors, dwinunknownprotos, dwoutoctets, dwoutucastpkts, dwoutnucastpkts, dwoutdiscards, dwouterrors, dwoutqlen, dwdescrlen: dword; bdescr: mib_ifdescr; end; pmib_iftable = ^mib_iftable; mib_iftable = packed record dwnumentries: dword; table: array[0..0] of mib_ifrow; end; tadapterstatus = record dwtype, dwoperstatus: dword; bdescr: mib_ifdescr; end; tadapterstatuses = array of tadapterstatus; function getiftable(piftable: pmib_iftable; pdwsize: pulong; border: bool): dword; stdcall; external 'iphlpapi.dll'; function getadaptertypestring(const dwtype: dword): string; function getgetadapterstatusstring(const dwoperstatus: dword): string; procedure scanadapters(var adapterstatuses: tadapterstatuses); implementation var dwsize: dword; pmibiftable: pmib_iftable; function getadaptertypestring(const dwtype: dword): string; begin case dwtype of mib_if_type_other: result := '其他'; mib_if_type_ethernet: result := '以太网'; mib_if_type_tokenring: result := '令牌环'; mib_if_type_fddi: result := 'fddi'; mib_if_type_ppp: result := 'ppp'; mib_if_type_loopback: result := '回路'; mib_if_type_slip: result := 'slip'; end; end; function getgetadapterstatusstring(const dwoperstatus: dword): string; begin case dwoperstatus of mib_if_oper_status_non_operational: result := '掉线'; mib_if_oper_status_unreachable: result := '不可达'; mib_if_oper_status_disconnected: result := '断开'; mib_if_oper_status_connecting: result := '连接中'; mib_if_oper_status_connected: result := '已连接'; mib_if_oper_status_operational: result := '连通'; end; end; procedure scanadapters(var adapterstatuses: tadapterstatuses); var dwretval: dword; num, i: longint; begin dwretval := getiftable(pmibiftable, @dwsize, false); if dwretval = no_error then begin num := pmibiftable^.dwnumentries; if length(adapterstatuses) <> num then setlength(adapterstatuses, num); for i := low(adapterstatuses) to high(adapterstatuses) do begin adapterstatuses[i].dwtype := pmibiftable^.table[i].dwtype; adapterstatuses[i].dwoperstatus := pmibiftable^.table[i].dwoperstatus; adapterstatuses[i].bdescr := pmibiftable^.table[i].bdescr; end; end; end; initialization getiftable(nil, @dwsize, false); getmem(pmibiftable, dwsize); finalization freemem(pmibiftable); end. unit mainform; interface uses classes, controls, forms, extctrls, comctrls, adputils; type tfrmmain = class(tform) tmrefresh: ttimer; lvadapters: tlistview; procedure tmrefreshtimer(sender: tobject); private fadapterstatuses: tadapterstatuses; fadapternum: integer; procedure refreshadapterstatuses; public { public declarations } end; var frmmain: tfrmmain; implementation {$r *.dfm} procedure tfrmmain.refreshadapterstatuses; var num, i: integer; begin scanadapters(fadapterstatuses); num := length(fadapterstatuses); if num = fadapternum then begin for i := 0 to num - 1 do lvadapters.items[i].subitems[1] := getgetadapterstatusstring(fadapterstatuses[i].dwoperstatus); end else begin lvadapters.items.beginupdate; lvadapters.items.clear; for i := 0 to num - 1 do begin lvadapters.items.add; lvadapters.items[i].caption := getadaptertypestring(fadapterstatuses[i].dwtype); lvadapters.items[i].subitems.add(fadapterstatuses[i].bdescr); lvadapters.items[i].subitems.add(getgetadapterstatusstring(fadapterstatuses[i].dwoperstatus)); end; lvadapters.items.endupdate; fadapternum := num; end; end; procedure tfrmmain.tmrefreshtimer(sender: tobject); begin refreshadapterstatuses; end; end. | | |
|