如何得到默认的网关 ( 积分: 39 )

  • 主题发起人 主题发起人 qiaoairenhongme
  • 开始时间 开始时间
Q

qiaoairenhongme

Unregistered / Unconfirmed
GUEST, unregistred user!
function GetNTGateway:TStrings;
var RegIniFile:TRegIniFile;
SList:TStringList;
I,size:Integer;
Str:String;
Buffer:array[0..255] of char;
begin
Result:=TStringList.Create;
SList:=TStringList.Create;
RegIniFile:=TRegIniFile.Create;
try
RegIniFile.RootKey:=HKEY_LOCAL_MACHINE;
if RegIniFile.OpenKey('SYSTEM/CurrentControlSet/Services/Tcpip/Parameters/Interfaces',false) then
begin
RegIniFile.ReadSections(SList);
RegIniFile.CloseKey;
for I:=0 to SList.Count-1 do
begin
RegIniFile.RootKey:=HKEY_LOCAL_MACHINE;
if RegIniFile.OpenKey('SYSTEM/CurrentControlSet/Services/Tcpip/Parameters/Interfaces/'+SList.Strings,false) then
begin
Size:=RegIniFile.GetDataSize('DefaultGateway') mod 255;
RegIniFile.ReadBinaryData('DefaultGateway',Buffer,size);
Str:=buffer;
if Str<>'' then Result.Append(Str);
end;
RegIniFile.CloseKey;
end;
end;
finally
RegIniFile.Free;
FreeAndNil(SList);
end;
end;

function Get98Gateway:TStrings;
var RegIniFile:TRegIniFile;
SList:TStringList;
I:Integer;
Str:String;
begin
Result:=TStringList.Create;
SList:=TStringList.Create;
RegIniFile:=TRegIniFile.Create;
try
RegIniFile.RootKey:=HKEY_LOCAL_MACHINE;
if RegIniFile.OpenKey('/System/CurrentControlSet/Services/Class/NetTrans',false) then
begin
RegIniFile.ReadSections(SList);
for I:=0 to SList.Count-1 do
begin
Str:=Trim(RegIniFile.ReadString(SList.Strings,'DriverDesc',''));
if UpperCase(Str)='TCP/IP' then
begin
Str:=RegIniFile.ReadString(SList.Strings,'DefaultGateway','');
Result.Append(Str);
Break;
end;
end;
end;
finally
RegIniFile.Free;
FreeAndNil(SList);
end;
end;

function GetDefaultGateway:String;
var Strs:TStrings;
begin
Result:='';Strs:=nil;
if Win32Platform=VER_PLATFORM_WIN32_WINDOWS then Strs:=Get98Gateway;
if Win32Platform=VER_PLATFORM_WIN32_NT then Strs:=GetNTGateway;
if Assigned(Strs) and (Strs.Count>0) then Result:=Strs[0];
Freeandnil(Strs);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetDefaultGateWay);
end;
 
后退
顶部