ProTagon
ProTagon

Reputation: 21

Inno Setup - delete the installer after the install process

As part of the copy protection of my game, the installer needs to delete itself after the installation process. This code:

[Code]
procedure MyAfterInstall();
begin
  DeleteFile('F:\TEST_SETUP\setup.exe');
end;

...does nothing because the setup runs. Is there a solution to run an 'command line' or cmd that gets the full path of the installer (it could be everywhere on the disc of the client) and delete it after the install?

Upvotes: 2

Views: 3949

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202642

If you need to delete the installer immediately after the installation, you have to implement some custom solution. As you have already found yourself, executable cannot delete itself as the executable is locked when it is running.

You can implement a small tool that you install by your installer. The tool will be run by the installer, when an installation finishes. The tool will keep running silently and trying to delete the installer, until is succeeds.

[Files]
; Install the tool
Source: "zapself.exe"; DestDir: "{app}"

[Run]
; Run the tool and pass the installer path
Filename: "{app}\zapself.exe"; Parameters: "{srcexe}"

Actually you do not need to build an .exe for this. A simple batch file created on-the-fly will do, for an example, see Unload a .NET DLL from an unmanaged process.


Need to say that I really do not understand why you are trying to do this.

Upvotes: 0

Pratik Vakil
Pratik Vakil

Reputation: 81

Add following method in your [CODE] section and you are all set...

[CODE]
procedure CurStepChanged(CurStep: TSetupStep);
var
  strContent: String;
  intErrorCode: Integer;
  strSelf_Delete_BAT: String;
begin
  if CurStep=ssDone then
  begin
    strContent := ':try_delete' + #13 + #10 +
          'del "' + ExpandConstant('{srcexe}') + '"' + #13 + #10 +
          'if exist "' + ExpandConstant('{srcexe}') + '" goto try_delete' + #13 + #10 +
          'del %0';

    strSelf_Delete_BAT := ExtractFilePath(ExpandConstant('{tmp}')) + 'SelfDelete.bat';
    SaveStringToFile(strSelf_Delete_BAT, strContent, False);
    Exec(strSelf_Delete_BAT, '', '', SW_HIDE, ewNoWait, intErrorCode);
  end;
end;

Upvotes: 7

Related Questions