How to end a process using inno setup?
-
14-02-2021 - |
Question
I want to kill a process using inno setup.i want to check whether the window is open before i start installing the setup.
can i do this by searching windows name? please help me with some sample code to kill the process
Solution
The best option is to use a mutex to see if it's still running using AppMutex
. One way to close it is to find the window handle then just post a simple WM_CLOSE message.
There are other options like the Files in use extension and PSVince
Also see this article for a bit more information.
OTHER TIPS
in the [UninstallRun] section, you can add the following code:
Filename: {sys}\taskkill.exe; Parameters: "/f /im MyProcess.exe"; Flags: skipifdoesntexist runhidden
This is something I did to prompt the user to close a program if it was found running.
function CheckProcessRunning( aProcName,
aProcDesc: string ): boolean;
var
ShellResult: boolean;
ResultCode: integer;
cmd: string;
sl: TStringList;
f: string;
d: string;
begin
cmd := 'for /f "delims=," %%i ' +
'in (''tasklist /FI "IMAGENAME eq ' + aProcName + '" /FO CSV'') ' +
'do if "%%~i"=="' + aProcName + '" exit 1';
f := 'CheckProc.cmd';
d := AddBackSlash( ExpandConstant( '{tmp}' ));
sl := TStringList.Create;
sl.Add( cmd );
sl.Add( 'exit /0' );
sl.SaveToFile( d + f );
sl.Free;
Result := true;
while ( Result ) do
begin
ResultCode := 1;
ShellResult := Exec( f,
'',
d,
SW_HIDE,
ewWaitUntilTerminated,
ResultCode );
Result := ResultCode > 0;
if Result and
( MsgBox( aProcDesc + ' is running. This program must be closed to continue.'#13#10 +
'Please close it and click Yes to retry or click No to cancel this installer.',
mbConfirmation,
MB_YESNO ) <> IDYES ) then
Break;
end;
DeleteFile( d + f );
end;
I used it in the following special code sections as follows:
// Perform some initializations. Return False to abort setup
function InitializeSetup: Boolean;
begin
// Do not use any user defined vars in here such as {app}
InitializeGlobals;
Result := not ( CheckProcessRunning( 'bcb.exe', 'C++ Builder' ) or
CheckProcessRunning( 'delphi32.exe', 'Delphi' ) or
CheckProcessRunning( 'bds.exe', 'Rad Studio' ));
end;
function InitializeUninstall: Boolean;
begin
InitializeGlobals;
Result := not ( CheckProcessRunning( 'bcb.exe', 'C++ Builder' ) or
CheckProcessRunning( 'delphi32.exe', 'Delphi' ) or
CheckProcessRunning( 'bds.exe', 'Rad Studio' ));
end;
A less complicated method would be to run a batch file which uses task kill
ie: if you wanted to kill notepad before uninstalling
taskkill /IM notepad.exe