如何获取各网卡本地连接的速度 ( 积分: 200 )

  • 主题发起人 主题发起人 BUCK FORLA
  • 开始时间 开始时间
B

BUCK FORLA

Unregistered / Unconfirmed
GUEST, unregistred user!
如标题:如何获取各网卡本地连接的速度??
请放出完整的 demo..谢谢
 
不明白你所谓的“完整 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;
 
顺便说一句,网卡的速度跟网速没虾米关系,仅取决于硬件本身,如果是千兆网卡那它的网速就是千兆的;不知楼主所谓的“网卡本地连接的速度”指的是什么?不要告诉我你指的是数据流量,如果是那样的话我就感到太悲哀了...
 
如果是获取所有网卡的速度,就把 GetLocalSpeed 中循环部分的 Break 去掉,你自己再定义一个 DWORD 类型的数组接收所有网卡速度的就行了:
type
TLocalSpeed = array[0..255] of DWORD;

procedure GetLocalSpeed(var Speeds: TLocalSpeed; var nNumberOfAdapter: Integer);
var
size, i: DWORD;
pmit: PMIB_IFTABLE;
begin
size := 0;
GetIfTable(nil, @size, False);
GetMem(pmit, size);
if GetIfTable(pmit, @size, True) <> NO_ERROR then Exit;
nNumberOfAdapter := 0;
i := pmit^.dwNumEntries;
for i := 0 to i - 1 do
begin
if pmit^.table.dwType = MIB_IF_TYPE_ETHERNET then
begin
Speeds[nNumberOfAdapter] := pmit^.table.dwSpeed;
Inc(nNumberOfAdapter);
end;
end;
FreeMem(pmit);
end;
 
连接速度和流量是2回事
 
To lovezyp:
你说的没错。
 
后退
顶部