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.
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;
Users browsing this forum: No registered users and 8 guests