Title: Change the IP address in runtime
Question: The problem: change the ip address of a network card without reboot the pc.
Answer:
procedure TForm1.Button1Click(Sender: TObject);
function ChangeIp(ConnectionName,Ip,Netmask,Gateway:string):boolean;
function ExecAndWait(FileName:String; Visibility:integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
Resultado: DWord;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes}
nil, { pointer to thread security attributes}
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Resultado);
Result := Resultado;
end;
end;
begin
Result:=( ExecAndWait( 'netsh interface ip set address "'+
ConnectionName+'" '+
' static '+Ip+ ' '+NetMask +' '+GateWay+' 1',sw_hide)=0 );
end;
begin
if NOT ChangeIp( 'Local Area Connection',
'192.168.10.22',
'255.255.255.0',
'192.168.10.90') then showmessage('Error changing IP')
else showmessage('Ip Changed without errors');
end;