How to execute a batch file on uninstall in Inno S

2020-03-25 15:18发布

问题:

I'm using code from How do you execute command line tools without using batch file in Inno Setup response to execute all my batch files on installation (before, after).

No I want to execute them just when user click "YES" to uninstaller, but can't find a way to do it. It executes before the confirmation

Here is my code from [Code] section:

function InitializeUninstall(): Boolean;
var
  ResultCode : Integer;    
begin
  Result := True;
  Exec(ExpandConstant('{app}\scripts\unset.bat'), '', '',
       SW_HIDE, ewWaitUntilTerminated, ResultCode); 
end; 

回答1:

Move your code to the CurUninstallStepChanged(usUninstall). That event is triggered after the confirmation of uninstallation.

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  ResultCode : Integer;    
begin
  if CurUninstallStep = usUninstall then
  begin
    Exec(ExpandConstant('{app}\scripts\unset.bat'), '', '',
         SW_HIDE, ewWaitUntilTerminated, ResultCode); 
  end;
end;

Though it's easier to use [UninstallRun] section.

[UninstallRun]
Filename: "{app}\scripts\unset.bat"; Flags: runhidden

The section is also processed after the confirmation, but before any files are uninstalled. See Uninstallation order.


Note that in general, you should not use batch files. You better script everything in Pascal code. That way you get much more robust code and error handling.