Dynamically changing Static IP addesses for the same machine though Delphi code

I want code in  Delphi to assign static IP addresses to my network card with out going through the Ntework connection properties dialog so that I can change my static IP addresses through the application
Venkas
LVL 1
venksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RadikalQ3Commented:
Hi venks

Using netsh:

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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
venksAuthor Commented:
I am using Delphi 7 & i could not find netsh.pas in my system.With out that  I am not able to compile
Venks
RadikalQ3Commented:
Netsh is a Windows command, not a Delphi unit...
:) do not include nothing in the uses line, just put the code in a TButton onclick event and run :)
(sorry... maybe is my poor english...)
venksAuthor Commented:
RadikalQ3
Thanks for the answer.It works & am awarding the points to you
Venks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.