贴点代码给你,看能不能帮助你
unit Link;
interface
function GateWay:string;
procedure IfLink;
implementation
uses
main,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Registry,
Grids;
function GateWay:string;
var
Reg :TRegistry;
MyStr : TStrings;
buffer : array[0..1024] of byte;
i:integer;
info:TRegKeyInfo ;
str:string;
begin
Result:='0';
Reg :=TRegistry.Create;
Mystr:=Tstringlist.Create;
try
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if Reg.OpenKey('SOFTWARE/Microsoft/Windows NT/CurrentVersion/NetworkCards',false) then
begin
if reg.GetKeyInfo(info) then
begin
reg.GetKeyNames(mystr);
str:='SOFTWARE/Microsoft/Windows NT/CurrentVersion/NetworkCards/'+Mystr.Strings[0];
end;
end;
finally
Reg.CloseKey;
Mystr.Free;
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if reg.OpenKey(str,false) then
begin
str:=reg.ReadString('ServiceName');
end;
Reg.CloseKey;
Reg.RootKey :=HKEY_LOCAL_MACHINE;
if reg.OpenKey('SYSTEM/CurrentControlSet/Services/'+str+'/Parameters/Tcpip/',false) then
begin
str:='';
for i:= 1 to reg.ReadBinaryData('defaultGateway',buffer,sizeof(buffer)) do
str:=str+chr(dword(buffer[i-1]));
end;
if str=chr(0) then
begin
str:='';
for i:= 1 to reg.ReadBinaryData('DhcpDefaultGateway',buffer,sizeof(buffer)) do
str:=str+chr(dword(buffer[i-1]));
end;
Reg.CloseKey;
reg.Free;
if (str=char(0)) or (str='') then
str:='66.218.71.88';
result:=str;
end;
end;
procedure IfLink;
begin
with mainForm do begin
ICMP.ReceiveTimeout:=10;
try
ICMP.Host := GateWay;
ICMP.Ping;
linklabel.caption:='正常';
linkLabel.Font.Color:=clGreen;
except
linklabel.caption:='断开';
mainform.linkLabel.Font.Color:=clred;
end;
end;
end;
end.