CodeAutomation.iss 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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
  8. DisableWelcomePage=no
  9. CreateAppDir=no
  10. DisableProgramGroupPage=yes
  11. DefaultGroupName=My Program
  12. UninstallDisplayIcon={app}\MyProg.exe
  13. OutputDir=userdocs:Inno Setup Examples Output
  14. [Code]
  15. {--- SQLDMO ---}
  16. const
  17. SQLServerName = 'localhost';
  18. SQLDMOGrowth_MB = 0;
  19. procedure SQLDMOButtonOnClick(Sender: TObject);
  20. var
  21. SQLServer, Database, DBFile, LogFile: Variant;
  22. IDColumn, NameColumn, Table: Variant;
  23. begin
  24. if MsgBox('Setup will now connect to Microsoft SQL Server ''' + SQLServerName + ''' via a trusted connection and create a database. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  25. Exit;
  26. { Create the main SQLDMO COM Automation object }
  27. try
  28. SQLServer := CreateOleObject('SQLDMO.SQLServer');
  29. except
  30. RaiseException('Please install Microsoft SQL server connectivity tools first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  31. end;
  32. { Connect to the Microsoft SQL Server }
  33. SQLServer.LoginSecure := True;
  34. SQLServer.Connect(SQLServerName);
  35. MsgBox('Connected to Microsoft SQL Server ''' + SQLServerName + '''.', mbInformation, mb_Ok);
  36. { Setup a database }
  37. Database := CreateOleObject('SQLDMO.Database');
  38. Database.Name := 'Inno Setup';
  39. DBFile := CreateOleObject('SQLDMO.DBFile');
  40. DBFile.Name := 'ISData1';
  41. DBFile.PhysicalName := 'c:\program files\microsoft sql server\mssql\data\IS.mdf';
  42. DBFile.PrimaryFile := True;
  43. DBFile.FileGrowthType := SQLDMOGrowth_MB;
  44. DBFile.FileGrowth := 1;
  45. Database.FileGroups.Item('PRIMARY').DBFiles.Add(DBFile);
  46. LogFile := CreateOleObject('SQLDMO.LogFile');
  47. LogFile.Name := 'ISLog1';
  48. LogFile.PhysicalName := 'c:\program files\microsoft sql server\mssql\data\IS.ldf';
  49. Database.TransactionLog.LogFiles.Add(LogFile);
  50. { Add the database }
  51. SQLServer.Databases.Add(Database);
  52. MsgBox('Added database ''' + Database.Name + '''.', mbInformation, mb_Ok);
  53. { Setup some columns }
  54. IDColumn := CreateOleObject('SQLDMO.Column');
  55. IDColumn.Name := 'id';
  56. IDColumn.Datatype := 'int';
  57. IDColumn.Identity := True;
  58. IDColumn.IdentityIncrement := 1;
  59. IDColumn.IdentitySeed := 1;
  60. IDColumn.AllowNulls := False;
  61. NameColumn := CreateOleObject('SQLDMO.Column');
  62. NameColumn.Name := 'name';
  63. NameColumn.Datatype := 'varchar';
  64. NameColumn.Length := '64';
  65. NameColumn.AllowNulls := False;
  66. { Setup a table }
  67. Table := CreateOleObject('SQLDMO.Table');
  68. Table.Name := 'authors';
  69. Table.FileGroup := 'PRIMARY';
  70. { Add the columns and the table }
  71. Table.Columns.Add(IDColumn);
  72. Table.Columns.Add(NameColumn);
  73. Database.Tables.Add(Table);
  74. MsgBox('Added table ''' + Table.Name + '''.', mbInformation, mb_Ok);
  75. end;
  76. {--- IIS ---}
  77. const
  78. IISServerName = 'localhost';
  79. IISServerNumber = '1';
  80. IISURL = 'http://127.0.0.1';
  81. procedure IISButtonOnClick(Sender: TObject);
  82. var
  83. IIS, WebSite, WebServer, WebRoot, VDir: Variant;
  84. ErrorCode: Integer;
  85. begin
  86. 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
  87. Exit;
  88. { Create the main IIS COM Automation object }
  89. try
  90. IIS := CreateOleObject('IISNamespace');
  91. except
  92. RaiseException('Please install Microsoft IIS first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  93. end;
  94. { Connect to the IIS server }
  95. WebSite := IIS.GetObject('IIsWebService', IISServerName + '/w3svc');
  96. WebServer := WebSite.GetObject('IIsWebServer', IISServerNumber);
  97. WebRoot := WebServer.GetObject('IIsWebVirtualDir', 'Root');
  98. { (Re)create a virtual dir }
  99. try
  100. WebRoot.Delete('IIsWebVirtualDir', 'innosetup');
  101. WebRoot.SetInfo();
  102. except
  103. end;
  104. VDir := WebRoot.Create('IIsWebVirtualDir', 'innosetup');
  105. VDir.AccessRead := True;
  106. VDir.AppFriendlyName := 'Inno Setup';
  107. VDir.Path := 'C:\inetpub\innosetup';
  108. VDir.AppCreate(True);
  109. VDir.SetInfo();
  110. MsgBox('Created virtual directory ''' + VDir.Path + '''.', mbInformation, mb_Ok);
  111. { Write some html and display it }
  112. if MsgBox('Setup will now write some HTML and display the virtual directory. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  113. Exit;
  114. ForceDirectories(VDir.Path);
  115. SaveStringToFile(VDir.Path + '/index.htm', '<html><body>Inno Setup rocks!</body></html>', False);
  116. if not ShellExecAsOriginalUser('open', IISURL + '/innosetup/index.htm', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode) then
  117. MsgBox('Can''t display the created virtual directory: ''' + SysErrorMessage(ErrorCode) + '''.', mbError, mb_Ok);
  118. end;
  119. {--- MSXML ---}
  120. const
  121. XMLURL = 'http://jrsoftware.github.io/issrc/ISHelp/isxfunc.xml';
  122. XMLFileName = 'isxfunc.xml';
  123. XMLFileName2 = 'isxfuncmodified.xml';
  124. procedure MSXMLButtonOnClick(Sender: TObject);
  125. var
  126. XMLHTTP, XMLDoc, NewNode, RootNode: Variant;
  127. Path: String;
  128. begin
  129. if MsgBox('Setup will now use MSXML to download XML file ''' + XMLURL + ''' and save it to disk.'#13#13'Setup will then load, modify and save this XML file. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  130. Exit;
  131. { Create the main MSXML COM Automation object }
  132. try
  133. XMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP');
  134. except
  135. RaiseException('Please install MSXML first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  136. end;
  137. { Download the XML file }
  138. XMLHTTP.Open('GET', XMLURL, False);
  139. XMLHTTP.Send();
  140. Path := ExpandConstant('{src}\');
  141. XMLHTTP.responseXML.Save(Path + XMLFileName);
  142. MsgBox('Downloaded the XML file and saved it as ''' + XMLFileName + '''.', mbInformation, mb_Ok);
  143. { Load the XML File }
  144. XMLDoc := CreateOleObject('MSXML2.DOMDocument');
  145. XMLDoc.async := False;
  146. XMLDoc.resolveExternals := False;
  147. XMLDoc.load(Path + XMLFileName);
  148. if XMLDoc.parseError.errorCode <> 0 then
  149. RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason);
  150. MsgBox('Loaded the XML file.', mbInformation, mb_Ok);
  151. { Modify the XML document }
  152. NewNode := XMLDoc.createElement('isxdemo');
  153. RootNode := XMLDoc.documentElement;
  154. RootNode.appendChild(NewNode);
  155. RootNode.lastChild.text := 'Hello, World';
  156. { Save the XML document }
  157. XMLDoc.Save(Path + XMLFileName2);
  158. MsgBox('Saved the modified XML as ''' + XMLFileName2 + '''.', mbInformation, mb_Ok);
  159. end;
  160. {--- Word ---}
  161. procedure WordButtonOnClick(Sender: TObject);
  162. var
  163. Word: Variant;
  164. begin
  165. if MsgBox('Setup will now check whether Microsoft Word is running. Do you want to continue?', mbInformation, mb_YesNo) = idNo then
  166. Exit;
  167. { Try to get an active Word COM Automation object }
  168. try
  169. Word := GetActiveOleObject('Word.Application');
  170. except
  171. end;
  172. if VarIsEmpty(Word) then
  173. MsgBox('Microsoft Word is not running.', mbInformation, mb_Ok)
  174. else
  175. MsgBox('Microsoft Word is running.', mbInformation, mb_Ok)
  176. end;
  177. {--- Windows Firewall ---}
  178. const
  179. NET_FW_IP_VERSION_ANY = 2;
  180. NET_FW_SCOPE_ALL = 0;
  181. procedure FirewallButtonOnClick(Sender: TObject);
  182. var
  183. Firewall, Application: Variant;
  184. begin
  185. 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
  186. Exit;
  187. { Create the main Windows Firewall COM Automation object }
  188. try
  189. Firewall := CreateOleObject('HNetCfg.FwMgr');
  190. except
  191. RaiseException('Please install Windows Firewall first.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  192. end;
  193. { Add the authorization }
  194. Application := CreateOleObject('HNetCfg.FwAuthorizedApplication');
  195. Application.Name := 'Setup';
  196. Application.IPVersion := NET_FW_IP_VERSION_ANY;
  197. Application.ProcessImageFileName := ExpandConstant('{srcexe}');
  198. Application.Scope := NET_FW_SCOPE_ALL;
  199. Application.Enabled := True;
  200. Firewall.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(Application);
  201. MsgBox('Setup is now an authorized application for the current profile', mbInformation, mb_Ok);
  202. end;
  203. {---}
  204. procedure CreateButton(ALeft, ATop: Integer; ACaption: String; ANotifyEvent: TNotifyEvent);
  205. begin
  206. with TButton.Create(WizardForm) do begin
  207. Left := ALeft;
  208. Top := ATop;
  209. Width := WizardForm.CancelButton.Width;
  210. Height := WizardForm.CancelButton.Height;
  211. Caption := ACaption;
  212. OnClick := ANotifyEvent;
  213. Parent := WizardForm.WelcomePage;
  214. end;
  215. end;
  216. procedure InitializeWizard();
  217. var
  218. Left, LeftInc, Top, TopInc: Integer;
  219. begin
  220. Left := WizardForm.WelcomeLabel2.Left;
  221. LeftInc := WizardForm.CancelButton.Width + ScaleX(8);
  222. TopInc := WizardForm.CancelButton.Height + ScaleY(8);
  223. Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height - 4*TopInc;
  224. CreateButton(Left, Top, '&SQLDMO...', @SQLDMOButtonOnClick);
  225. CreateButton(Left + LeftInc, Top, '&Firewall...', @FirewallButtonOnClick);
  226. Top := Top + TopInc;
  227. CreateButton(Left, Top, '&IIS...', @IISButtonOnClick);
  228. Top := Top + TopInc;
  229. CreateButton(Left, Top, '&MSXML...', @MSXMLButtonOnClick);
  230. Top := Top + TopInc;
  231. CreateButton(Left, Top, '&Word...', @WordButtonOnClick);
  232. end;