不明白你所谓的“完整 demo”是什么意思,反正我下面的代码你帖到程序里就能用,单位是 bps:
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;
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 GetIfEntry(pIfRow: PMIB_IFROW): DWORD; stdcall;
external 'iphlpapi.dll';
function GetLocalSpeed: DWORD;
var
size, i: DWORD;
pmit: PMIB_IFTABLE;
begin
Result := 0;
size := 0;
GetIfTable(nil, @size, False);
GetMem(pmit, size);
if GetIfTable(pmit, @size, True) <> NO_ERROR then Exit;
i := pmit^.dwNumEntries;
for i := 0 to i - 1 do
begin
if pmit^.table.dwType = MIB_IF_TYPE_ETHERNET then
begin
Result := pmit^.table.dwSpeed;
Break;
end;
end;
FreeMem(pmit);
end;