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;
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 pAdapters: PMIB_IFTABLE);
implementation
var
dwSize: DWORD;
pMibIfTable: PMIB_IFTABLE;
function GetAdapterTypeString(const dwType: DWORD): string;
begin
case dwType of
MIB_IF_TYPE_OTHER: Result := 'OTHER(其他)';
MIB_IF_TYPE_ETHERNET: Result := 'ETHERNET(以太网)';
MIB_IF_TYPE_TOKENRING: Result := 'TOKENRING(令牌环)';
MIB_IF_TYPE_FDDI: Result := 'FDDI(光纤分布式数据接口)';
MIB_IF_TYPE_PPP: Result := 'PPP(端对端协议)';
MIB_IF_TYPE_LOOPBACK: Result := 'LOOPBACK(回路)';
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 pAdapters: PMIB_IFTABLE);
var
dwRetVal: DWORD;
begin
dwRetVal := GetIfTable(pMibIfTable, @dwSize, False);
if dwRetVal = NO_ERROR then
pAdapters := pMibIfTable
else pAdapters := nil;
end;
initialization
GetIfTable(nil, @dwSize, False);
GetMem(pMibIfTable, dwSize);
finalization
FreeMem(pMibIfTable);
end.