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