CodeAutomation.iss 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ; -- CodeAutomation.iss --
  2. ;
  3. ; This script shows how to use IDispatch based COM Automation objects.
  4. [Setup]
  5. AppName=My Program
  6. AppVersion=1.5
  7. WizardStyle=modern dynamic
  8. DisableWelcomePage=no
  9. CreateAppDir=no
  10. Uninstallable=no
  11. DisableProgramGroupPage=yes
  12. DefaultGroupName=My Program
  13. OutputDir=userdocs:Inno Setup Examples Output
  14. [Code]
  15. {--- IIS ---}
  16. const
  17. IISServerName = 'localhost';
  18. IISServerNumber = '1';
  19. IISURL = 'http://127.0.0.1';
  20. procedure IISButtonOnClick(Sender: TObject);
  21. var
  22. IIS, WebSite, WebServer, WebRoot, VDir: Variant;
  23. ErrorCode: Integer;
  24. begin
  25. if MsgBox('Setup will now connect to Microsoft IIS Server ''' + IISServerName + ''' and create a virtual directory. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  26. Exit;
  27. { Create the main IIS COM Automation object }
  28. try
  29. IIS := CreateOleObject('IISNamespace');
  30. except
  31. RaiseException('Please install Microsoft IIS first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  32. end;
  33. { Connect to the IIS server }
  34. WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
  35. WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
  36. WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
  37. { (Re)create a virtual dir }
  38. try
  39. WebRoot.Delete('IIsWebVirtualDir', 'innosetup');
  40. WebRoot.SetInfo();
  41. except
  42. end;
  43. VDir := WebRoot.Create('IIsWebVirtualDir', 'innosetup');
  44. VDir.AccessRead := True;
  45. VDir.AppFriendlyName := 'Inno Setup';
  46. VDir.Path := 'C:\inetpub\innosetup';
  47. VDir.AppCreate(True);
  48. VDir.SetInfo();
  49. MsgBox('Created virtual directory ''' + VDir.Path + '''.', mbInformation, mb_Ok);
  50. { Write some html and display it }
  51. if MsgBox('Setup will now write some HTML and display the virtual directory. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  52. Exit;
  53. ForceDirectories(VDir.Path);
  54. SaveStringToFile(VDir.Path + '/index.htm', '<html><body>Inno Setup rocks!</body></html>', False);
  55. if not ShellExecAsOriginalUser('open', IISURL + '/innosetup/index.htm', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode) then
  56. MsgBox('Can''t display the created virtual directory: ''' + SysErrorMessage(ErrorCode) + '''.', mbError, mb_Ok);
  57. end;
  58. {--- MSXML ---}
  59. const
  60. XMLURL = 'https://jrsoftware.github.io/issrc/ISHelp/isxfunc.xml';
  61. XMLFileName = 'isxfunc.xml';
  62. XMLFileName2 = 'isxfuncmodified.xml';
  63. procedure MSXMLButtonOnClick(Sender: TObject);
  64. var
  65. XMLHTTP, XMLDoc, NewNode, RootNode: Variant;
  66. Path: String;
  67. begin
  68. if MsgBox('Setup will now use MSXML to download XML file ''' + XMLURL + ''' and save it to the source folder.'#13#13'Setup will then load, modify and save this XML file. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  69. Exit;
  70. { Create the main MSXML COM Automation object }
  71. try
  72. XMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP');
  73. except
  74. RaiseException('Please install MSXML first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  75. end;
  76. { Download the XML file }
  77. XMLHTTP.Open('GET', XMLURL, False);
  78. XMLHTTP.Send();
  79. Path := ExpandConstant('{src}\');
  80. XMLHTTP.responseXML.Save(Path + XMLFileName);
  81. MsgBox('Downloaded the XML file and saved it as ''' + XMLFileName + '''.', mbInformation, mb_Ok);
  82. { Load the XML File }
  83. XMLDoc := CreateOleObject('MSXML2.DOMDocument');
  84. XMLDoc.async := False;
  85. XMLDoc.resolveExternals := False;
  86. XMLDoc.load(Path + XMLFileName);
  87. if XMLDoc.parseError.errorCode <> 0 then
  88. RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason);
  89. MsgBox('Loaded the XML file.', mbInformation, mb_Ok);
  90. { Modify the XML document }
  91. NewNode := XMLDoc.createElement('isxdemo');
  92. RootNode := XMLDoc.documentElement;
  93. RootNode.appendChild(NewNode);
  94. RootNode.lastChild.text := 'Hello, World';
  95. { Save the XML document }
  96. XMLDoc.Save(Path + XMLFileName2);
  97. MsgBox('Saved the modified XML as ''' + XMLFileName2 + '''.', mbInformation, mb_Ok);
  98. end;
  99. {--- Word ---}
  100. procedure WordButtonOnClick(Sender: TObject);
  101. var
  102. Word: Variant;
  103. begin
  104. if MsgBox('Setup will now check whether Microsoft Word is running. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  105. Exit;
  106. { Try to get an active Word COM Automation object }
  107. try
  108. Word := GetActiveOleObject('Word.Application');
  109. except
  110. end;
  111. if VarIsEmpty(Word) then
  112. MsgBox('Microsoft Word is not running.', mbInformation, mb_Ok)
  113. else
  114. MsgBox('Microsoft Word is running.', mbInformation, mb_Ok)
  115. end;
  116. {--- Windows Firewall ---}
  117. const
  118. NET_FW_IP_VERSION_ANY = 2;
  119. NET_FW_SCOPE_ALL = 0;
  120. procedure FirewallButtonOnClick(Sender: TObject);
  121. var
  122. Firewall, Application: Variant;
  123. begin
  124. if MsgBox('Setup will now add itself to Windows Firewall as an authorized application for the current profile (' + GetUserNameString + '). Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  125. Exit;
  126. { Create the main Windows Firewall COM Automation object }
  127. try
  128. Firewall := CreateOleObject('HNetCfg.FwMgr');
  129. except
  130. RaiseException('Please install Windows Firewall first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  131. end;
  132. { Add the authorization }
  133. Application := CreateOleObject('HNetCfg.FwAuthorizedApplication');
  134. Application.Name := 'Setup';
  135. Application.IPVersion := NET_FW_IP_VERSION_ANY;
  136. Application.ProcessImageFileName := ExpandConstant('{srcexe}');
  137. Application.Scope := NET_FW_SCOPE_ALL;
  138. Application.Enabled := True;
  139. Firewall.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(Application);
  140. MsgBox('Setup is now an authorized application for the current profile', mbInformation, mb_Ok);
  141. end;
  142. {--- Unzip ---}
  143. const
  144. ZipURL = 'https://jrsoftware.org/download.php/iscrypt.zip';
  145. ZipFileName = 'iscrypt.zip';
  146. ZipSHA256 = '0569ffe1677ba699d07063a902d48c2f92c8e88669bdc13118f5808c30e998bc';
  147. SHCONTCH_NOPROGRESSBOX = 4;
  148. SHCONTCH_RESPONDYESTOALL = 16;
  149. procedure UnzipButtonOnClick(Sender: TObject);
  150. var
  151. Shell, ZipFolder, TargetFolder: Variant;
  152. ZipPath, TargetPath: String;
  153. begin
  154. if MsgBox('Setup will now download Zip file ''' + ZipURL + ''' and save it to a temporary folder.'#13#13'Setup will then unzip this Zip file to the source folder. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  155. Exit;
  156. { Download the Zip file }
  157. DownloadTemporaryFile(ZipURL + '?dontcount=1', ZipFileName, ZipSHA256, nil);
  158. ZipPath := ExpandConstant('{tmp}\' + ZipFileName);
  159. MsgBox('Downloaded the Zip file and saved it as ''' + ZipPath + '''.', mbInformation, mb_Ok);
  160. { Unzip the Zip file to the source folder }
  161. Shell := CreateOleObject('Shell.Application');
  162. ZipFolder := Shell.NameSpace(ZipPath);
  163. if VarIsClear(ZipFolder) then
  164. RaiseException(Format('Zip file ''%s'' does not exist or cannot be opened', [ZipPath]));
  165. TargetPath := ExpandConstant('{src}');
  166. TargetFolder := Shell.NameSpace(TargetPath);
  167. if VarIsClear(TargetFolder) then
  168. RaiseException(Format('Target ''%s'' does not exist', [TargetPath]));
  169. TargetFolder.CopyHere(ZipFolder.Items, SHCONTCH_NOPROGRESSBOX or SHCONTCH_RESPONDYESTOALL);
  170. MsgBox('Unzipped the Zip file to ''' + TargetPath + '''.', mbInformation, mb_Ok);
  171. end;
  172. {---}
  173. procedure CreateButton(ALeft, ATop: Integer; ACaption: String; ANotifyEvent: TNotifyEvent);
  174. begin
  175. with TNewButton.Create(WizardForm) do begin
  176. Left := ALeft;
  177. Top := ATop;
  178. Width := WizardForm.CancelButton.Width;
  179. Height := WizardForm.CancelButton.Height;
  180. Caption := ACaption;
  181. OnClick := ANotifyEvent;
  182. Parent := WizardForm.WelcomePage;
  183. end;
  184. end;
  185. procedure InitializeWizard();
  186. var
  187. Left, Top, TopInc: Integer;
  188. begin
  189. WizardForm.WelcomeLabel2.AdjustHeight;
  190. Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height + ScaleY(8);
  191. Left := WizardForm.WelcomeLabel2.Left;
  192. TopInc := WizardForm.CancelButton.Height + ScaleY(8);
  193. CreateButton(Left, Top, '&Firewall...', @FirewallButtonOnClick);
  194. Top := Top + TopInc;
  195. CreateButton(Left, Top, '&IIS...', @IISButtonOnClick);
  196. Top := Top + TopInc;
  197. CreateButton(Left, Top, '&MSXML...', @MSXMLButtonOnClick);
  198. Top := Top + TopInc;
  199. CreateButton(Left, Top, '&Word...', @WordButtonOnClick);
  200. Top := Top + TopInc;
  201. CreateButton(Left, Top, '&Unzip...', @UnzipButtonOnClick);
  202. end;