unit Inet;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1Select(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
IList: TStringList;
implementation
{$R *.dfm}
function ReadIP(key:string):string;
const
ifs='SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces';
var
reg: HKey;
DataType, DataSize: DWORD;
s: string;
begin
result:='';
s:=ifs+'\'+key;
if RegOpenKeyEx(HKEY_local_machine,PAnsiChar(s),0,KEY_QUERY_VALUE,reg)=ERROR_SUCCESS then
begin
RegQueryValueEx(reg,'IPAddress',nil,@DataType,nil,@DataSize);
SetLength(s, DataSize-1);
RegQueryValueEx(reg,'IPAddress',nil,@DataType,PByte(@s[1]),@DataSize);
result:=s;
end;
RegCloseKey(reg);
end;
procedure SetIP(key,value:string);
const
ifs='SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces';
var
reg: HKey;
begin
if RegOpenKeyEx(HKEY_LOCAL_MACHINE,PAnsiChar(ifs+'\'+key),0,KEY_SET_VALUE,reg)=ERROR_SUCCESS then
RegSetValueEx(reg,'IPAddress',0,REG_MULTI_SZ,PAnsiChar(value),Length(value));
RegCloseKey(reg);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if ComboBox1.Items.Count=0 then Exit;
SetIP(IList[ComboBox1.itemIndex],Edit1.Text);
Edit1.Text:=ReadIP(IList[ComboBox1.ItemIndex]);
end;
procedure TForm1.FormCreate(Sender: TObject);
const
nwc='SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards';
var
card,reg: HKey;
DataType, DataSize: DWORD;
buf: array[0 .. 1000] of char;
index: longint;
s: string;
begin
IList:=TStringList.Create;
RegOpenKey(HKEY_local_machine, nwc, card);
index:=0;
while RegEnumKey(card, index, buf, Sizeof(buf))=ERROR_SUCCESS do
begin
if RegOpenKeyEx(card,buf,0,KEY_QUERY_VALUE,reg)=ERROR_SUCCESS then
begin
RegQueryValueEx(reg,'Description',nil,@DataType,nil,@DataSize);
SetLength(s, DataSize-1);
RegQueryValueEx(reg,'Description',nil,@DataType,PByte(@s[1]),@DataSize);
ComboBox1.Items.Add(s);
RegQueryValueEx(reg,'ServiceName',nil,@DataType,nil,@DataSize);
SetLength(s, DataSize-1);
RegQueryValueEx(reg,'ServiceName',nil,@DataType,PByte(@s[1]),@DataSize);
IList.Add(s);
RegCloseKey(reg);
end;
inc(index);
end;
RegCloseKey(card);
With ComboBox1 do if Items.Count>0 then begin
ItemIndex:=0;
Edit1.Text:=ReadIP(IList[0]);
end;
end;
procedure TForm1.ComboBox1Select(Sender: TObject);
begin
if ComboBox1.Items.Count=0 then Exit;
Edit1.Text:=ReadIP(IList[ComboBox1.ItemIndex]);
Button1.Enabled:=true;
end;
end.