Delphi开发的IP地址修改工具

用Delphi进行开发的,直接修改注册表,需重启电脑后才生效

procedure TForm1.SpeedButton3Click(Sender: TObject);

var

  reg:TRegistry;

  IP,WG,ZY:array [0..255] of char;

  ID,CardNum:string;

  Psize:Dword;

  DNS:string;

begin

  reg:=TRegistry.Create;

  name:='';

  reg.RootKey:=HKEY_LOCAL_MACHINE;

  ID:=RGcardList.Items.Strings[RGCardList.itemindex];

  cardNum:=copy(ID,pos(';',ID)+1,length(ID));

  reg.CloseKey;

  if length(CardNum) < 2 then

    CardNum:='00'+CardNum;

  if LengTh(CardNum) < 3 then

    CardNum:='0'+CardNum;

  reg.OpenKey('SYSTEM\ControlSet'+CardNum+'\Services',false);

  reg.OpenKey('Tcpip\Parameters\Interfaces',false);

  ID:=CardIdLt.Strings[CardIDLt.IndexOf(ID)+1];

  reg.OpenKey(id,false);

  DNS:=Reg.ReadString('NameServer');

  RegqueryValueEx(reg.CurrentKey,'IPAddress',nil,nil,@IP,@Psize);

  Edit1.Text:=copy(IP,0,strlen(IP));

  RegqueryValueEx(reg.CurrentKey,'SubnetMask',nil,nil,@ZY,@Psize);

  Edit2.Text:=copy(ZY,0,strlen(ZY));

  RegqueryValueEx(reg.CurrentKey,'DefaultGateway',nil,nil,@WG,@Psize);

  Edit3.Text:=copy(WG,0,strlen(WG));

  RegqueryValueEx(reg.CurrentKey,'SubnetMask',nil,nil,@ZY,@Psize);

  Edit2.Text:=copy(ZY,0,strlen(ZY));

  RegqueryValueEx(reg.CurrentKey,'IPAddress',nil,nil,@ZY,@Psize);

  Edit1.Text:=copy(ZY,0,strlen(ZY));

  Reg.CloseKey;

  reg.Free;

  if pos(',',DNS)<=0 then begin Edit4.Text:=DNS;  end;

  if pos(',',DNS)>0 then

    begin

      Edit4.Text:=Copy(DNS,0,Pos(',',DNS)-1);

      Edit5.Text:=Copy(DNS,Pos(',',DNS)+1,Length(DNS));

    end;

end;

 读取IP

procedure TForm1.setIPAddr;

var

  reg:TRegistry;

  name:array [0..255] of char;

  DNS,ID,CardNum:string;

begin

  reg:=TRegistry.Create;

  StrLcopy(name,pchar(Edit6.Text),length(Edit6.Text));

  reg.RootKey:=HKEY_LOCAL_MACHINE;

  ID:=RGcardList.Items.Strings[RGCardList.itemindex];

  cardNum:=copy(ID,pos(';',ID)+1,length(ID));

  reg.CloseKey;

  if length(CardNum) < 2 then

    CardNum:='00'+CardNum;

  if LengTh(CardNum) < 3 then

    CardNum:='0'+CardNum;

  reg.OpenKey('SYSTEM\ControlSet'+CardNum+'\Services',false);

  reg.OpenKey('Tcpip\Parameters\Interfaces',false);

  ID:=CardIdLt.Strings[CardIDLt.IndexOf(ID)+1];

  reg.OpenKey(id,false);

  RegSetValueEx(reg.CurrentKey,'IPAddress',0,REG_MULTI_SZ,@name,13);



  StrLcopy(name,pchar(Edit7.Text),length(Edit7.Text));

  reg.RootKey:=HKEY_LOCAL_MACHINE;

  ID:=RGcardList.Items.Strings[RGCardList.itemindex];

  cardNum:=copy(ID,pos(';',ID)+1,length(ID));

  reg.CloseKey;

  if length(CardNum) < 2 then

    CardNum:='00'+CardNum;

  if LengTh(CardNum) < 3 then

    CardNum:='0'+CardNum;

  reg.OpenKey('SYSTEM\ControlSet'+CardNum+'\Services',false);

  reg.OpenKey('Tcpip\Parameters\Interfaces',false);

  ID:=CardIdLt.Strings[CardIDLt.IndexOf(ID)+1];

  reg.OpenKey(id,false);

  RegSetValueEx(reg.CurrentKey,'SubnetMask',0,REG_MULTI_SZ,@name,13);



  StrLcopy(name,pchar(Edit8.Text),length(Edit8.Text));

  reg.RootKey:=HKEY_LOCAL_MACHINE;

  ID:=RGcardList.Items.Strings[RGCardList.itemindex];

  cardNum:=copy(ID,pos(';',ID)+1,length(ID));

  reg.CloseKey;

  if length(CardNum) < 2 then

    CardNum:='00'+CardNum;

  if LengTh(CardNum) < 3 then

    CardNum:='0'+CardNum;

  reg.OpenKey('SYSTEM\ControlSet'+CardNum+'\Services',false);

  reg.OpenKey('Tcpip\Parameters\Interfaces',false);

  ID:=CardIdLt.Strings[CardIDLt.IndexOf(ID)+1];

  reg.OpenKey(id,false);

  RegSetValueEx(reg.CurrentKey,'DefaultGateway',0,REG_MULTI_SZ,@name,13);



  IF (Trim(Edit9.Text)='') And (Trim(Edit10.Text)='') Then Exit;

  IF (TRim(Edit9.Text)<>'') And (Trim(Edit10.Text)<>'') then

    begin

      DNS:=Trim(Edit9.Text)+','+TRim(Edit10.Text);

     end;

   IF (Trim(Edit9.Text)<>'') And (Trim(Edit10.Text)='') then

     begin

        DNS:=Trim(Edit9.Text);

      end;

   IF (Trim(Edit9.Text)='') And (Trim(Edit10.Text)<>'') then

     begin

        DNS:=Trim(Edit10.Text);

    end;

  StrLcopy(name,pchar(DNS),length(DNS));

  reg.RootKey:=HKEY_LOCAL_MACHINE;

  ID:=RGcardList.Items.Strings[RGCardList.itemindex];

  cardNum:=copy(ID,pos(';',ID)+1,length(ID));

  reg.CloseKey;

  if length(CardNum) < 2 then

    CardNum:='00'+CardNum;

  if LengTh(CardNum) < 3 then

    CardNum:='0'+CardNum;

  reg.OpenKey('SYSTEM\ControlSet'+CardNum+'\Services',false);

  reg.OpenKey('Tcpip\Parameters\Interfaces',false);

  ID:=CardIdLt.Strings[CardIDLt.IndexOf(ID)+1];

  reg.OpenKey(id,false);

 // RegSetValueEx(reg.CurrentKey,'NameServer',0,REG_MULTI_SZ,@name,13);

  reg.WriteString('NameServer',DNS);

  Reg.CloseKey;

  reg.Free;

end;

 设置IP

 

使用中还有一些问题,待完善

你可能感兴趣的:(Delphi)