DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Making an app restart itself

 
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming
View previous topic :: View next topic  
Author Message
azarroth
Member


Joined: 07 May 2003
Posts: 9
Location: Wisconsin, USA

PostPosted: 13/09/03 01:15    Post subject: Making an app restart itself Reply with quote

Any one know how to have an app auto restart itself? Like if you change the location of / or create a new database with an app and the changes don't take effect until the app is closed and reopened. Basically make the restart virtually transparent to the user.
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 206

PostPosted: 13/09/03 10:48    Post subject: Reply with quote

At the end of the main program you have to call another program, which that one should:
  1. Wait for the caller application's process to terminate
  2. Restarts the application
Back to top
View user's profile Send e-mail Visit poster's website
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 206

PostPosted: 13/09/03 13:59    Post subject: Reply with quote

Here is a the source of a console application that I described it in my last post.

Code:
program WaitRestartApp;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows,
  TLHelp32;

function FindExecutableProcessID(const FullPath: String): DWORD;
var
  Snapshot: THandle;
  ProcessEntry32: TPROCESSENTRY32;
begin
  Result := 0;
  Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  try
    if Snapshot <> 0 then
    begin
      ProcessEntry32.dwSize := SizeOf(TPROCESSENTRY32);
      if Process32First(Snapshot, ProcessEntry32) then
        repeat
          if StrIComp(PChar(FullPath), ProcessEntry32.szExeFile) = 0 then
            Result := ProcessEntry32.th32ProcessID;
        until (Result <> 0) or not Process32Next(Snapshot, ProcessEntry32);
    end;
  finally
    CloseHandle(Snapshot);
  end;
end;

function WaitForProcessID(ProcessID: DWORD): Boolean;
var
  ProcessHandle: THandle;
begin
  Result := False;
  ProcessHandle := OpenProcess(SYNCHRONIZE, False, ProcessID);
  if ProcessHandle <> 0 then
    try
      WaitForSingleObject(ProcessHandle, INFINITE);
      Result := True;
    finally
      CloseHandle(ProcessHandle);
    end;
end;

function Execute(const CommandLine: String): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  Result := CreateProcess(nil, PChar(CommandLine), nil, nil, False,
            CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
            StartupInfo, ProcessInfo);
end;

var
  ProcessID: DWORD;
  CommandLine: String;
  I: Integer;
begin
  if ParamCount > 0 then
  begin
    ProcessID := FindExecutableProcessID(ParamStr(1));
    if ProcessID <> 0 then
      WaitForProcessID(ProcessID);
    CommandLine := ParamStr(1);
    for I := 2 to ParamCount do
      CommandLine := CommandLine + ' ' + ParamStr(I);
    Execute(CommandLine);
  end;
end.

And here is an example of usage, supposed both applications are in the same folder:

Code:
function ExecuteHidden(const CommandLine: String): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_HIDE;
  Result := CreateProcess(nil, PChar(CommandLine), nil, nil, False,
            CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS, nil, nil,
            StartupInfo, ProcessInfo);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  CommandLine: String;
  FullPathToAuxApp: String;
begin
  FullPathToAuxApp := ExtractFilePath(ParamStr(0)) + 'WaitRestartApp.exe';
  CommandLine := Format('"%s" "%s"', [FullPathToAuxApp, ParamStr(0)]);
  if ExecuteHidden(CommandLine) then
    Close
  else
    ShowMessage('Error: Cannot restart');
end;

Hope that it helps.
Kambiz
Back to top
View user's profile Send e-mail Visit poster's website
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group