如何编程修改IP并且立即生效? (66分)

  • 主题发起人 主题发起人 delphi5198
  • 开始时间 开始时间
D

delphi5198

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,这个问题困扰我一个月了,CSDN上也没有人回答对。
注意:是修改Win2000不是的Win98,Win98我就不问了。
 
如果是win2k/xp就可以。执行netsh interface 命令。下面是一个例子。
//form文件:

object FrmChangeIp: TFrmChangeIp
Left = 232
Top = 194
BorderIcons = []
BorderStyle = bsSingle
Caption = '动态改变IP'
ClientHeight = 186
ClientWidth = 256
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 12
object Label1: TLabel
Left = 48
Top = 48
Width = 36
Height = 12
Caption = 'IP地址'
end
object Label2: TLabel
Left = 48
Top = 80
Width = 24
Height = 12
Caption = '掩码'
end
object Label3: TLabel
Left = 48
Top = 112
Width = 24
Height = 12
Caption = '网关'
end
object Label5: TLabel
Left = 41
Top = 19
Width = 48
Height = 12
Caption = '连接名称'
end
object Edit1: TEdit
Left = 104
Top = 16
Width = 121
Height = 20
TabOrder = 0
Text = '本地连接'
end
object Edit2: TEdit
Left = 104
Top = 48
Width = 121
Height = 20
TabOrder = 1
Text = '192.168.0.30'
end
object Edit3: TEdit
Left = 104
Top = 80
Width = 121
Height = 20
TabOrder = 2
Text = '255.255.255.0'
end
object Edit4: TEdit
Left = 104
Top = 112
Width = 121
Height = 20
TabOrder = 3
Text = '192.168.0.1'
end
end
//执行代码
procedure TFrmChangeIp.BtnChangeClick(Sender: TObject);
var
str:string;
begin
str:='netsh interface ip set address '+edit1.text +' static '+edit2.text+' '+edit3.text+' '+edit4.text+' 1';
winexec(PChar(str),sw_hide);
end;
 
记得还要打开"remote registery service"服务
 
net start "remote registery service"
netsh interface ip set address ......
net stop "remote registery service"

因为remote registery service是一个很不安全的服务,所以平常我都是不让他自己启动的
 
多人接受答案了。
 
后退
顶部