CodeExample1.iss 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ; -- CodeExample1.iss --
  2. ;
  3. ; This script shows various things you can achieve using a [Code] section.
  4. [Setup]
  5. AppName=My Program
  6. AppVersion=1.5
  7. WizardStyle=modern
  8. DisableWelcomePage=no
  9. DefaultDirName={code:MyConst}\My Program
  10. DefaultGroupName=My Program
  11. UninstallDisplayIcon={app}\MyProg.exe
  12. InfoBeforeFile=Readme.txt
  13. OutputDir=userdocs:Inno Setup Examples Output
  14. [Files]
  15. Source: "MyProg.exe"; DestDir: "{app}"; Check: MyProgCheck; BeforeInstall: BeforeMyProgInstall('MyProg.exe'); AfterInstall: AfterMyProgInstall('MyProg.exe')
  16. Source: "MyProg.chm"; DestDir: "{app}"; Check: MyProgCheck; BeforeInstall: BeforeMyProgInstall('MyProg.chm'); AfterInstall: AfterMyProgInstall('MyProg.chm')
  17. Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
  18. [Icons]
  19. Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
  20. [Code]
  21. var
  22. MyProgChecked: Boolean;
  23. MyProgCheckResult: Boolean;
  24. FinishedInstall: Boolean;
  25. function InitializeSetup(): Boolean;
  26. begin
  27. Log('InitializeSetup called');
  28. Result := MsgBox('InitializeSetup:' #13#13 'Setup is initializing. Do you really want to start setup?', mbConfirmation, MB_YESNO) = idYes;
  29. if Result = False then
  30. MsgBox('InitializeSetup:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);
  31. end;
  32. procedure InitializeWizard;
  33. begin
  34. Log('InitializeWizard called');
  35. end;
  36. <event('InitializeWizard')>
  37. procedure InitializeWizard2;
  38. begin
  39. Log('InitializeWizard2 called');
  40. end;
  41. procedure DeinitializeSetup();
  42. var
  43. FileName: String;
  44. ResultCode: Integer;
  45. begin
  46. Log('DeinitializeSetup called');
  47. if FinishedInstall then begin
  48. if MsgBox('DeinitializeSetup:' #13#13 'The [Code] scripting demo has finished. Do you want to uninstall My Program now?', mbConfirmation, MB_YESNO) = idYes then begin
  49. FileName := ExpandConstant('{uninstallexe}');
  50. if not Exec(FileName, '', '', SW_SHOWNORMAL, ewNoWait, ResultCode) then
  51. MsgBox('DeinitializeSetup:' #13#13 'Execution of ''' + FileName + ''' failed. ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
  52. end else
  53. MsgBox('DeinitializeSetup:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);
  54. end;
  55. end;
  56. procedure CurStepChanged(CurStep: TSetupStep);
  57. begin
  58. Log('CurStepChanged(' + IntToStr(Ord(CurStep)) + ') called');
  59. if CurStep = ssPostInstall then
  60. FinishedInstall := True;
  61. end;
  62. procedure CurInstallProgressChanged(CurProgress, MaxProgress: Integer);
  63. begin
  64. Log('CurInstallProgressChanged(' + IntToStr(CurProgress) + ', ' + IntToStr(MaxProgress) + ') called');
  65. end;
  66. function NextButtonClick(CurPageID: Integer): Boolean;
  67. var
  68. ResultCode: Integer;
  69. begin
  70. Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
  71. case CurPageID of
  72. wpSelectDir:
  73. MsgBox('NextButtonClick:' #13#13 'You selected: ''' + WizardDirValue + '''.', mbInformation, MB_OK);
  74. wpSelectProgramGroup:
  75. MsgBox('NextButtonClick:' #13#13 'You selected: ''' + WizardGroupValue + '''.', mbInformation, MB_OK);
  76. wpReady:
  77. begin
  78. if MsgBox('NextButtonClick:' #13#13 'Using the script, files can be extracted before the installation starts. For example we could extract ''MyProg.exe'' now and run it.' #13#13 'Do you want to do this?', mbConfirmation, MB_YESNO) = idYes then begin
  79. ExtractTemporaryFile('myprog.exe');
  80. if not ExecAsOriginalUser(ExpandConstant('{tmp}\myprog.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode) then
  81. MsgBox('NextButtonClick:' #13#13 'The file could not be executed. ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
  82. end;
  83. BringToFrontAndRestore();
  84. MsgBox('NextButtonClick:' #13#13 'The normal installation will now start.', mbInformation, MB_OK);
  85. end;
  86. end;
  87. Result := True;
  88. end;
  89. function BackButtonClick(CurPageID: Integer): Boolean;
  90. begin
  91. Log('BackButtonClick(' + IntToStr(CurPageID) + ') called');
  92. Result := True;
  93. end;
  94. function ShouldSkipPage(PageID: Integer): Boolean;
  95. begin
  96. Log('ShouldSkipPage(' + IntToStr(PageID) + ') called');
  97. { Skip wpInfoBefore page; show all others }
  98. case PageID of
  99. wpInfoBefore:
  100. Result := True;
  101. else
  102. Result := False;
  103. end;
  104. end;
  105. procedure CurPageChanged(CurPageID: Integer);
  106. begin
  107. Log('CurPageChanged(' + IntToStr(CurPageID) + ') called');
  108. case CurPageID of
  109. wpWelcome:
  110. MsgBox('CurPageChanged:' #13#13 'Welcome to the [Code] scripting demo. This demo will show you some possibilities of the scripting support.' #13#13 'The scripting engine used is RemObjects Pascal Script by Carlo Kok. See http://www.remobjects.com/ps for more information.', mbInformation, MB_OK);
  111. wpFinished:
  112. MsgBox('CurPageChanged:' #13#13 'Welcome to final page of this demo. Click Finish to exit.', mbInformation, MB_OK);
  113. end;
  114. end;
  115. function PrepareToInstall(var NeedsRestart: Boolean): String;
  116. begin
  117. Log('PrepareToInstall() called');
  118. if MsgBox('PrepareToInstall:' #13#13 'Setup is preparing to install. Using the script you can install any prerequisites, abort Setup on errors, and request restarts. Do you want to return an error now?', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = idYes then
  119. Result := '<your error text here>.'
  120. else
  121. Result := '';
  122. end;
  123. function MyProgCheck(): Boolean;
  124. begin
  125. Log('MyProgCheck() called');
  126. if not MyProgChecked then begin
  127. MyProgCheckResult := MsgBox('MyProgCheck:' #13#13 'Using the script you can decide at runtime to include or exclude files from the installation. Do you want to install MyProg.exe and MyProg.chm to ' + ExtractFilePath(CurrentFileName) + '?', mbConfirmation, MB_YESNO) = idYes;
  128. MyProgChecked := True;
  129. end;
  130. Result := MyProgCheckResult;
  131. end;
  132. procedure BeforeMyProgInstall(S: String);
  133. begin
  134. Log('BeforeMyProgInstall(''' + S + ''') called');
  135. MsgBox('BeforeMyProgInstall:' #13#13 'Setup is now going to install ' + S + ' as ' + CurrentFileName + '.', mbInformation, MB_OK);
  136. end;
  137. procedure AfterMyProgInstall(S: String);
  138. begin
  139. Log('AfterMyProgInstall(''' + S + ''') called');
  140. MsgBox('AfterMyProgInstall:' #13#13 'Setup just installed ' + S + ' as ' + CurrentFileName + '.', mbInformation, MB_OK);
  141. end;
  142. function MyConst(Param: String): String;
  143. begin
  144. Log('MyConst(''' + Param + ''') called');
  145. Result := ExpandConstant('{autopf}');
  146. end;