To fanofdelphi:
1、今晚闲着没事,给你写了个较完整的程序。虽然我不知道 RAS 是个虾米玩意,但发现获得拨号流量比网卡的容易多了。程序应该没问题,我无法调试,你试一下。
2、界面上放两个 Label,Label1 用来显示拨号设备、名称什么的,Label2 用来实时显示流量、速度什么的;还需要一个 Timer(初始状态 Enabel = False),用来实时刷新 Label2。
const
rasapi32 = 'rasapi32.dll';
RAS_MaxEntryName = 256;
RAS_MaxDeviceType = 16;
RAS_MaxDeviceName = 128;
type
HRASCONN = LongWord;
RASCONN = record
dwSize: DWORD;
hrasconn: HRASCONN;
szEntryName: array[0..RAS_MaxEntryName] of Char;
szDeviceType: array[0.. RAS_MaxDeviceType] of Char;
szDeviceName: array[0.. RAS_MaxDeviceName] of Char;
end;
RAS_STATS = record
dwSize: DWORD;
dwBytesXmited: DWORD;
dwBytesRcved: DWORD;
dwFramesXmited: DWORD;
dwFramesRcved: DWORD;
dwCrcErr: DWORD;
dwTimeoutErr: DWORD;
dwAlignmentErr: DWORD;
dwHardwareOverrunErr: DWORD;
dwFramingErr: DWORD;
dwBufferOverrunErr: DWORD;
dwCompressionRatioIn: DWORD;
dwCompressionRatioOut: DWORD;
dwBps: DWORD;
dwConnectDuration: DWORD;
end;
function RasEnumConnections(var lprasconn: RASCONN; var lpcb: DWORD; var lpcConnections: DWORD): DWORD; stdcall; external rasapi32 name 'RasEnumConnectionsA';
function RasGetConnectionStatistics(hRasConn: HRASCONN; var Statistics: RAS_STATS): DWORD; stdcall; external rasapi32 name 'RasGetConnectionStatistics';
function GetActiveRasConnection: RASCONN;
var
dwCnnSize, dwRetries, dwCnnNum: DWORD;
begin
dwCnnSize := SizeOf(RASCONN);
dwRetries := 5;
while dwRetries > 0 do
begin
FillChar(Result, dwCnnSize, 0);
Result.dwSize := dwCnnSize;
if RasEnumConnections(Result, dwCnnSize, dwCnnNum) = 0 then Break;
Dec(dwRetries);
end;
end;
var
cnn: RASCONN;
procedure TForm1.Button1Click(Sender: TObject);
begin
cnn := GetActiveRasConnection;
if cnn.hrasconn = 0 then Exit;
Label1.Caption := '拨号名称: ' + cnn.szEntryName + #13 + '设备名: ' + cnn.szDeviceName;
Timer1.Enabled := not Timer1.Enabled;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
const
cb = SizeOf(RAS_STATS);
var
stat: RAS_STATS;
begin
if cnn.hrasconn = 0 then Exit;
FillChar(stat, cb, 0);
stat.dwSize := cb;
RasGetConnectionStatistics(cnn.hrasconn, stat);
Label2.Caption := '速率: ' + IntToStr(stat.dwBps) + ' bps' + #13 +
'发送: ' + IntToStr(stat.dwBytesXmited) + ' Bytes' + #13 +
'收到: ' + IntToStr(stat.dwBytesRcved) + ' Bytes';
end;
3、MSDN 2003 上有两个函数的说明,我翻译成了 Pascal,常量是我在头文件里找的。