CodePrepareToInstall.iss 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ; -- CodePrepareToInstall.iss --
  2. ;
  3. ; This script shows how the PrepareToInstall event function can be used to
  4. ; install prerequisites and handle any reboots in between, while remembering
  5. ; user selections across reboots.
  6. [Setup]
  7. AppName=My Program
  8. AppVersion=1.5
  9. WizardStyle=modern
  10. DefaultDirName={autopf}\My Program
  11. DefaultGroupName=My Program
  12. UninstallDisplayIcon={app}\MyProg.exe
  13. OutputDir=userdocs:Inno Setup Examples Output
  14. [Files]
  15. ; Place any prerequisite files here, for example:
  16. ; Source: "MyProg-Prerequisite-setup.exe"; Flags: dontcopy
  17. ; Place any regular files here, so *after* all your prerequisites.
  18. Source: "MyProg.exe"; DestDir: "{app}";
  19. Source: "MyProg.chm"; DestDir: "{app}";
  20. Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme;
  21. [Icons]
  22. Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
  23. [Code]
  24. const
  25. (*** Customize the following to your own name. ***)
  26. RunOnceName = 'My Program Setup restart';
  27. QuitMessageReboot = 'The installation of a prerequisite program was not completed. You will need to restart your computer to complete that installation.'#13#13'After restarting your computer, Setup will continue next time an administrator logs in.';
  28. QuitMessageError = 'Error. Cannot continue.';
  29. var
  30. Restarted: Boolean;
  31. function InitializeSetup(): Boolean;
  32. begin
  33. Restarted := ExpandConstant('{param:restart|0}') = '1';
  34. if not Restarted then begin
  35. Result := not RegValueExists(HKA, 'Software\Microsoft\Windows\CurrentVersion\RunOnce', RunOnceName);
  36. if not Result then
  37. MsgBox(QuitMessageReboot, mbError, mb_Ok);
  38. end else
  39. Result := True;
  40. end;
  41. function DetectAndInstallPrerequisites: Boolean;
  42. begin
  43. (*** Place your prerequisite detection and extraction+installation code below. ***)
  44. (*** Return False if missing prerequisites were detected but their installation failed, else return True. ***)
  45. //<your code here>
  46. //extraction example: ExtractTemporaryFile('MyProg-Prerequisite-setup.exe');
  47. Result := True;
  48. (*** Remove the following block! Used by this demo to simulate a prerequisite install requiring a reboot. ***)
  49. if not Restarted then
  50. RestartReplace(ParamStr(0), '');
  51. end;
  52. function Quote(const S: String): String;
  53. begin
  54. Result := '"' + S + '"';
  55. end;
  56. function AddParam(const S, P, V: String): String;
  57. begin
  58. if V <> '""' then
  59. Result := S + ' /' + P + '=' + V;
  60. end;
  61. function AddSimpleParam(const S, P: String): String;
  62. begin
  63. Result := S + ' /' + P;
  64. end;
  65. procedure CreateRunOnceEntry;
  66. var
  67. RunOnceData: String;
  68. begin
  69. RunOnceData := Quote(ExpandConstant('{srcexe}')) + ' /restart=1';
  70. RunOnceData := AddParam(RunOnceData, 'LANG', ExpandConstant('{language}'));
  71. RunOnceData := AddParam(RunOnceData, 'DIR', Quote(WizardDirValue));
  72. RunOnceData := AddParam(RunOnceData, 'GROUP', Quote(WizardGroupValue));
  73. if WizardNoIcons then
  74. RunOnceData := AddSimpleParam(RunOnceData, 'NOICONS');
  75. RunOnceData := AddParam(RunOnceData, 'TYPE', Quote(WizardSetupType(False)));
  76. RunOnceData := AddParam(RunOnceData, 'COMPONENTS', Quote(WizardSelectedComponents(False)));
  77. RunOnceData := AddParam(RunOnceData, 'TASKS', Quote(WizardSelectedTasks(False)));
  78. (*** Place any custom user selection you want to remember below. ***)
  79. //<your code here>
  80. RegWriteStringValue(HKA, 'Software\Microsoft\Windows\CurrentVersion\RunOnce', RunOnceName, RunOnceData);
  81. end;
  82. function PrepareToInstall(var NeedsRestart: Boolean): String;
  83. var
  84. ChecksumBefore, ChecksumAfter: String;
  85. begin
  86. ChecksumBefore := MakePendingFileRenameOperationsChecksum;
  87. if DetectAndInstallPrerequisites then begin
  88. ChecksumAfter := MakePendingFileRenameOperationsChecksum;
  89. if ChecksumBefore <> ChecksumAfter then begin
  90. CreateRunOnceEntry;
  91. NeedsRestart := True;
  92. Result := QuitMessageReboot;
  93. end;
  94. end else
  95. Result := QuitMessageError;
  96. end;
  97. function ShouldSkipPage(PageID: Integer): Boolean;
  98. begin
  99. Result := Restarted;
  100. end;