急问:如何获得及设置本机的ip地址,子网掩码,网关,dns服务器等。

duoduowy 2004-10-26 04:49:44
如题,各位大虾帮忙!~~~~~~~~~~
...全文
2151 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdsgajxlp 2005-01-06
  • 打赏
  • 举报
回复
up
Kshape 2005-01-04
  • 打赏
  • 举报
回复
这一段获得主机名字,IP地址等的代码,楼主看看
===============================================
unit hostName;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsock;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Label3: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Ip:string;
Ipstr:string;
Buffer:Array[1..32] of char;
i:integer;
WSData:TWSAdata;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then
begin
ShowMessage('WS2_32.DLL初始化失败!');
halt;
end;
try
if GetHostName(@Buffer[1],32)<>0 then
begin
ShowMessage('没有得到主机名!');
halt;
end;
except
ShowMessage('没有成功返回主机名');
halt;
end;
Host := GetHostByName(@Buffer[1]);
if Host = nil then
begin
ShowMessage('IP地址为空');
halt;
end
else
begin
Edit2.Text := Host.h_name;
Edit3.Text := Chr(Host.h_addrtype+64);
for i:=1 to 4 do
begin
Ip:=IntToStr(Ord(Host.h_addr^[i-1]));
ShowMessage('分段IP地址为:'+Ip);
if i<4 then
Ipstr := Ipstr + Ip + '.'
else
Edit1.Text:=Ipstr+Ip;
end;
end;
WSACleanup;
end;
end.
zglwxb 2005-01-04
  • 打赏
  • 举报
回复

狂顶--------------------顶
Kshape 2005-01-03
  • 打赏
  • 举报
回复
高,
连 WinExec(PChar('command.com /C route print >'+Temp),SW_HIDE);
都想出来了
rodmanchen99 2005-01-03
  • 打赏
  • 举报
回复
加这段
uses winsock;
windedge99 2004-12-17
  • 打赏
  • 举报
回复
用iphelp api
fan178 2004-12-14
  • 打赏
  • 举报
回复
我是菜鸟,我执行这段代码为什么报错呢,是不是需要再uses里面加什么?
maodelphi 2004-12-11
  • 打赏
  • 举报
回复
欢迎广大Delphi程序员到《delphi专题研讨会》到QQ群7568683
讨论
dreamonce 2004-12-10
  • 打赏
  • 举报
回复
高,
连 WinExec(PChar('command.com /C route print >'+Temp),SW_HIDE);
都想出来了
oushengfen 2004-11-12
  • 打赏
  • 举报
回复
up
Behard 2004-11-11
  • 打赏
  • 举报
回复
读取注册表即可
zhouhua206 2004-11-11
  • 打赏
  • 举报
回复
收藏
InfiniteHeart 2004-10-27
  • 打赏
  • 举报
回复
把Indy Misc页下第第13个控件IdIPWatch摆在你的窗体上
IdIPWatch1.localip是你的本地ip地址
其它的自己看吧
Rex_love_Burger 2004-10-27
  • 打赏
  • 举报
回复
设置本机的ip地址、子网掩码、网关:shellexecute(handle,'','netsh interface ip set address “本地连接”static 192.168.0.1 255.255.255.0 192.168.0.10 1',nil,nil,sw_hide)
同理可得修改DNS及wins等信息的方法.
相关网络命令如下:
set address name = ″本地连接″ source = static addr = 192.168.0.5 mask = 255.255.255.0
  set address name = ″本地连接″ gateway = 192.168.0.2 gwmetric = 1
  set dns name = ″本地连接″ source = static addr = 61.237.17.181
  add dns name = ″本地连接″ addr = 211.97.168.129
  add dns name = ″本地连接″ addr = 211.98.4.1
  set wins name = ″本地连接″ source = static addr = none


Bitter_fish 2004-10-27
  • 打赏
  • 举报
回复
收藏啊!
ksaiy 2004-10-27
  • 打赏
  • 举报
回复
子网隐码WIn98下:
procedure TForm1.Button1Click(Sender: TObject);
var
reg:TRegistry;
re:boolean;
mask:string;
begin
reg:=TRegistry.Create();
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
//open key
//win 9x
re:=Reg.OpenKey('System\CurrentControlSet\Services\Class\NetTrans\0000',
false);

if re then
begin
mask := Reg.Readstring('IPMask');
end
else
begin
edit1.text:='Failed to get mask';
reg.closekey();
reg.Free;
exit;
end;
finally
Reg.CloseKey;
Reg.Free;
end;
edit1.text:=mask;

end;

ksaiy 2004-10-27
  • 打赏
  • 举报
回复
取子网隐码:
procedure TForm1.Button1Click(Sender: TObject);
var
reg:TRegistry;
re:boolean;
device:string;
buf:pchar;
mask:string;
adapter:string;
begin
reg:=TRegistry.Create();
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
//open key
//win NT
//get bind device
re:=Reg.OpenKey('SYSTEM\CurrentControlSet\Services\Tcpip\Linkage',
false);

if re then
begin
getmem(buf,1024);
Reg.ReadBinaryData('Bind',buf^,1024);
device := strpas(buf);
freemem(buf);
end
else
begin
edit1.text:='Failed to get bind device';
reg.closekey();
reg.Free;
exit;
end;
reg.CloseKey;
//get adapter
adapter:=copy(device,2,length(device)-1);
showmessage(adapter);
adapter:=copy(adapter,POS('\',adapter)+1,length(adapter)-POS('\',adapter));
showmessage(adapter);
//query
re:=Reg.OpenKey('SYSTEM\CurrentControlSet\Services\'+
adapter+
'\Parameters\Tcpip',
false);
if re then
begin
getmem(buf,1024);
Reg.ReadBinaryData('SubnetMask',buf^,1024);
mask := strpas(buf);
freemem(buf);
end

else
mask:='Failed to get subnet mask';
finally
Reg.CloseKey;
Reg.Free;
end;
edit1.text:=device;
edit2.text:=mask;
end;
ksaiy 2004-10-27
  • 打赏
  • 举报
回复
MAC代码:
procedure TForm1.Button1Click(Sender: TObject);
Var
NCB : TNCB;
ADAPTER : TADAPTERSTATUS;
LANAENUM : TLANAENUM;
intIdx : Integer;
re : Char;
buf : String;
begin
Try
{ ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBENUM);
re := NetBios(@NCB);

// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;
}
{ Copyright (C) Bowman }
{ MatthewBowman@21cn.com }
// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[0];//important
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;

// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[0];////bowman
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;

buf := '';
For intIdx := 0 To 5 Do
buf := buf + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2)+'-';
edit1.Text := copy(buf,0,length(buf)-1);
Finally
End;

end;
zwb666 2004-10-26
  • 打赏
  • 举报
回复

获取路由、网关信息、网卡信息:
Procedure TForm1.Button1Click(Sender:TObject);
begin
Memo1.Lines.Clear;
if FileExists(Temp) then
DeleteFile(Temp);
WinExec(PChar('command.com /C route print >'+Temp),SW_HIDE);
while not FileExists(Temp) do
Sleep(1000)
Try
Memo1.Lines.LoadFromFile(Temp);
except
end;
end;


rjy206 2004-10-26
  • 打赏
  • 举报
回复
function LocalIP:string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;

begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;

program get_ip;
uses
winsock,sysutils;
VAR
ch : ARRAY[1..32] OF Char;
i : Integer;
WSData: TWSAData;
MyHost: PHostEnt;
begin
IF WSAstartup(2,wsdata)<>0 THEN
BEGIN
Writeln('can''t start Winsock: Error ',WSAGetLastError);
Halt(2);
END;
try
IF getHostName(@ch[1],32)<>0 THEN
BEGIN
Writeln('getHostName failed');
Halt(3);
END;
except
Writeln('getHostName failed');
halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
IF MyHost=NIL THEN
BEGIN
Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error
'+IntToStr(WSAGetLastError));
Halt(4);
END
ELSE
BEGIN
Write('address ');
FOR i:=1 TO 4 DO
BEGIN
Write(Ord(MyHost.h_addr^[i-1]));
IF i<4 THEN
then write('.')
ELSE
writeln;
END;
END;
end.
加载更多回复(2)

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧