Search code examples
batch-fileinstallationinno-setupuninstallation

Passing custom CLI parameters to Inno Setup uninstaller


I want to pre-configure my custom uninstall batch script with parameters passed to the unins000.exe. I already achieved it for the installer, but for some reason it doesnt work for the uninstaller.

Here is the Inno Setup Script:

[Run]
Filename: "{app}\resources\app\Setup\install.bat"; \
    Parameters: "python-3.10.7-amd64.exe {param:pysetup|standard}"

[UninstallRun]
Filename: "{app}\resources\app\Setup\uninstall.bat"; \
    Parameters: "python-3.10.7-amd64.exe {param:ole|2} {param:defxml|2} {param:pywin|2} {param:python|2}"

Note: The Run Section works (setup.exe /pysetuptype=...), but the UninstallRun section doesn't work (unins000.exe /ole=... /defxml=... /pywin=... /python=...)

Here is my uninstall.bat script:

@ECHO OFF

:OLE
IF "%2"=="1" GOTO :UNINOLE
IF "%2"=="0" GOTO :DEFXML

SET /p answer=Do you want to uninstall oletools? Y / N 
IF /i %answer:~,1% EQU Y GOTO :UNINOLE
IF /i %answer:~,1% EQU N GOTO :DEFXML
ECHO [Y]es / [N]o
GOTO OLE

:UNINOLE
ECHO Y | pip uninstall oletools[full]
ECHO.

:DEFXML
IF "%3"=="1" GOTO :UNINDEF
IF "%3"=="0" GOTO :PYWIN
SET /p answer=Do you want to uninstall defusedxml? Y / N 
IF /i %answer:~,1% EQU Y GOTO :UNINDEF
IF /i %answer:~,1% EQU N GOTO :PYWIN
ECHO [Y]es / [N]o
GOTO DEFXML

:UNINDEF
ECHO Y | pip uninstall defusedxml
ECHO.

:PYWIN
IF "%4"=="1" GOTO :UNINPYWIN
IF "%4"=="0" GOTO :UNINPY
SET /p answer=Do you want to uninstall pywin32? Y / N 
IF /i %answer:~,1% EQU Y GOTO :UNINPYWIN
IF /i %answer:~,1% EQU N GOTO :UNINPY
ECHO [Y]es / [N]o
GOTO PYWIN

:UNINPYWIN
ECHO Y | pip uninstall pywin32
ECHO.

:UNINPY
IF "%5"=="1" START /WAIT %1 /uninstall /passive EXIT
IF "%5"=="0" EXIT
SET /p answer=Do you want to run the Setup for Python v.3.10.7 to uninstall Python? Y / N 
IF /i %answer:~,1% EQU Y GOTO :PYSETUP
IF /i %answer:~,1% EQU N EXIT
ECHO [Y]es / [N]o
GOTO UNINPY

:PYSETUP
ECHO.
ECHO python-3.10.7-amd64.exe will start now
START /WAIT %1

EXIT

What do I have to change for this to work?


Solution

  • The constants in the UninstallRun section are resolved on install time. Not on uninstall time.

    If you need to use uninstall time parameters, you have code it in Pascal Script with use of Exec and ExpandConstant support functions and CurUninstallStepChanged event function.

    [Run]
    Filename: "{app}\resources\app\Setup\install.bat"; \
        Parameters: "python-3.10.7-amd64.exe {param:pysetup|standard}"
    
    [Code]
    procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
    var
      ResultCode: Integer;
      Filename, Params: string;
    begin
      if CurUninstallStep = usUninstall then
      begin
        Filename := ExpandConstant('{app}\resources\app\Setup\uninstall.bat');
        Params := ExpandConstant('python-3.10.7-amd64.exe {param:ole|2} {param:defxml|2} {param:pywin|2} {param:python|3}');
        if not Exec(Filename, Params, '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
        begin
          MsgBox(
            'Uninstallation failed with code ' + IntToStr(ResultCode),
            mbError, MB_OK);
        end;
      end;
    end.
    

    Related question: Inno Setup Scripted Constants in uninstaller