CodeExample1.iss 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. DefaultDirName={code:MyConst}\My Program
  8. DefaultGroupName=My Program
  9. UninstallDisplayIcon={app}\MyProg.exe
  10. InfoBeforeFile=Readme.txt
  11. OutputDir=userdocs:Inno Setup Examples Output
  12. [Files]
  13. Source: "MyProg.exe"; DestDir: "{app}"; Check: MyProgCheck; BeforeInstall: BeforeMyProgInstall('MyProg.exe'); AfterInstall: AfterMyProgInstall('MyProg.exe')
  14. Source: "MyProg.chm"; DestDir: "{app}"; Check: MyProgCheck; BeforeInstall: BeforeMyProgInstall('MyProg.chm'); AfterInstall: AfterMyProgInstall('MyProg.chm')
  15. Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
  16. [Icons]
  17. Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
  18. [Code]
  19. var
  20. MyProgChecked: Boolean;
  21. MyProgCheckResult: Boolean;
  22. FinishedInstall: Boolean;
  23. function InitializeSetup(): Boolean;
  24. begin
  25. Log('InitializeSetup called');
  26. Result := MsgBox('InitializeSetup:' #13#13 'Setup is initializing. Do you really want to start setup?', mbConfirmation, MB_YESNO) = idYes;
  27. if Result = False then
  28. MsgBox('InitializeSetup:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);
  29. end;
  30. procedure DeinitializeSetup();
  31. var
  32. FileName: String;
  33. ResultCode: Integer;
  34. begin
  35. Log('DeinitializeSetup called');
  36. if FinishedInstall then begin
  37. 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
  38. FileName := ExpandConstant('{uninstallexe}');
  39. if not Exec(FileName, '', '', SW_SHOWNORMAL, ewNoWait, ResultCode) then
  40. MsgBox('DeinitializeSetup:' #13#13 'Execution of ''' + FileName + ''' failed. ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
  41. end else
  42. MsgBox('DeinitializeSetup:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);
  43. end;
  44. end;
  45. procedure CurStepChanged(CurStep: TSetupStep);
  46. begin
  47. Log('CurStepChanged(' + IntToStr(Ord(CurStep)) + ') called');
  48. if CurStep = ssPostInstall then
  49. FinishedInstall := True;
  50. end;
  51. procedure CurInstallProgressChanged(CurProgress, MaxProgress: Integer);
  52. begin
  53. Log('CurInstallProgressChanged(' + IntToStr(CurProgress) + ', ' + IntToStr(MaxProgress) + ') called');
  54. end;
  55. function NextButtonClick(CurPageID: Integer): Boolean;
  56. var
  57. ResultCode: Integer;
  58. begin
  59. Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
  60. case CurPageID of
  61. wpSelectDir:
  62. MsgBox('NextButtonClick:' #13#13 'You selected: ''' + WizardDirValue + '''.', mbInformation, MB_OK);
  63. wpSelectProgramGroup:
  64. MsgBox('NextButtonClick:' #13#13 'You selected: ''' + WizardGroupValue + '''.', mbInformation, MB_OK);
  65. wpReady:
  66. begin
  67. 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
  68. ExtractTemporaryFile('myprog.exe');
  69. if not ExecAsOriginalUser(ExpandConstant('{tmp}\myprog.exe'), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode) then
  70. MsgBox('NextButtonClick:' #13#13 'The file could not be executed. ' + SysErrorMessage(ResultCode) + '.', mbError, MB_OK);
  71. end;
  72. BringToFrontAndRestore();
  73. MsgBox('NextButtonClick:' #13#13 'The normal installation will now start.', mbInformation, MB_OK);
  74. end;
  75. end;
  76. Result := True;
  77. end;
  78. function BackButtonClick(CurPageID: Integer): Boolean;
  79. begin
  80. Log('BackButtonClick(' + IntToStr(CurPageID) + ') called');
  81. Result := True;
  82. end;
  83. function ShouldSkipPage(PageID: Integer): Boolean;
  84. begin
  85. Log('ShouldSkipPage(' + IntToStr(PageID) + ') called');
  86. { Skip wpInfoBefore page; show all others }
  87. case PageID of
  88. wpInfoBefore:
  89. Result := True;
  90. else
  91. Result := False;
  92. end;
  93. end;
  94. procedure CurPageChanged(CurPageID: Integer);
  95. begin
  96. Log('CurPageChanged(' + IntToStr(CurPageID) + ') called');
  97. case CurPageID of
  98. wpWelcome:
  99. 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);
  100. wpFinished:
  101. MsgBox('CurPageChanged:' #13#13 'Welcome to final page of this demo. Click Finish to exit.', mbInformation, MB_OK);
  102. end;
  103. end;
  104. function PrepareToInstall(var NeedsRestart: Boolean): String;
  105. begin
  106. Log('PrepareToInstall() called');
  107. 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
  108. Result := '<your error text here>.'
  109. else
  110. Result := '';
  111. end;
  112. function MyProgCheck(): Boolean;
  113. begin
  114. Log('MyProgCheck() called');
  115. if not MyProgChecked then begin
  116. 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;
  117. MyProgChecked := True;
  118. end;
  119. Result := MyProgCheckResult;
  120. end;
  121. procedure BeforeMyProgInstall(S: String);
  122. begin
  123. Log('BeforeMyProgInstall(''' + S + ''') called');
  124. MsgBox('BeforeMyProgInstall:' #13#13 'Setup is now going to install ' + S + ' as ' + CurrentFileName + '.', mbInformation, MB_OK);
  125. end;
  126. procedure AfterMyProgInstall(S: String);
  127. begin
  128. Log('AfterMyProgInstall(''' + S + ''') called');
  129. MsgBox('AfterMyProgInstall:' #13#13 'Setup just installed ' + S + ' as ' + CurrentFileName + '.', mbInformation, MB_OK);
  130. end;
  131. function MyConst(Param: String): String;
  132. begin
  133. Log('MyConst(''' + Param + ''') called');
  134. Result := ExpandConstant('{pf}');
  135. end;