ElektroStudios
ElektroStudios

Reputation: 20464

Process this uninstaller code only if uninstall is demanded?

I have this code in the [Code] section, this code only runs when I run the uninstaller:

//////////////
// Uninstaller
//////////////

const
  DeleteFiles   = true;
  DeleteSubdirs = false;

// Initialize the Inno Setup Uninstaller skin style.
function InitializeUninstall: Boolean;
begin
  Result := True;
  LoadVCLStyle_UnInstall(ExpandConstant('{app}\uninstall.vsf'));
end;

// Deinitialize the Inno Setup Uninstaller skin style.
procedure DeinitializeUninstall();
begin
  UnLoadVCLStyles_UnInstall;
  UnloadDll(ExpandConstant('{app}\uninstall.dll'));
  DeleteFile(ExpandConstant('{app}\uninstall.dll'));
  DelTree(ExpandConstant('{app}\'), true, DeleteFiles, DeleteSubdirs);
end;

The problem is that the instructions inside the DeinitializeUninstall procedure of DeleteFile and DelTree runs even if the user choose Yes or No when the InnoSetup uninstaller asks the user whether really want to uninstall or not the software, I mean the user selection of this image:

enter image description here

Of course I understand what that procedure means and why my instructions runs even choosing No because the uninstaller is deinitialized whatever I choose, but just I can't find the right way to do this efficiently.

So I need to processs these two instructions below only if the user really demanded a uninstall choosing Yes, but also these instructions should run at the end of the uninstallation process (and that is the DeinitializeUninstall procedure I supose):

DeleteFile(ExpandConstant('{app}\uninstall.dll'));
DelTree(ExpandConstant('{app}\'), true, DeleteFiles, DeleteSubdirs);

How I can do it?

Upvotes: 1

Views: 221

Answers (1)

ElektroStudios
ElektroStudios

Reputation: 20464

The solution, thanks to @TLama for the CurUninstallStepChanged hint and thanks to this other answer where I've taken an example to realize this:

//////////////
// Uninstaller
//////////////


Const
  DeleteFiles   = True;  // Determines whether to delete all the files of the {app} dir.
  DeleteSubdirs = False; // Determines whether to delete all the sub-folders of the {app} dir.

Var
  UninstallIsDemanded: Boolean; // Determines whether the user accepted or denied the uninstallation prompt.
  UninstallSuccess   : Boolean; // Determines whether the uninstallation succeeded.


// Occurs when the uninstaller current page changes.
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin

  if CurUninstallStep = usUninstall then begin
      UninstallIsDemanded:= True;
  end;

  if CurUninstallStep = usDone then begin
      UninstallSuccess:= True;
  end;  

end;


// Deletes the VCL skin dll file.
procedure DeleteSkin();
begin
  DeleteFile(ExpandConstant('{app}\uninstall.dll'));
end;


// Deletes the 'app}' file/folder contents.
procedure DeleteApplication(DeleteFiles: Boolean; DeleteSubdirs: Boolean);
begin
  DelTree(ExpandConstant('{app}\'), true, DeleteFiles, DeleteSubdirs);
end;


// Initialize the Inno Setup Uninstaller skin style.
function InitializeUninstall: Boolean;
begin
  Result := True;
  LoadVCLStyle_UnInstall(ExpandConstant('{app}\uninstall.vsf'));
end;


// Deinitialize the Inno Setup Uninstaller skin style.
procedure DeinitializeUninstall();
begin
  UnLoadVCLStyles_UnInstall;
  UnloadDll(ExpandConstant('{app}\uninstall.dll'));

  if UninstallSuccess = True then begin
    DeleteSkin();
    DeleteApplication(DeleteFiles, DeleteSubdirs);
  end;

end;

Upvotes: 1

Related Questions