CodeAutomation.iss 9.4 KB

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