CodeAutomation2.iss 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ; -- CodeAutomation2.iss --
  2. ;
  3. ; This script shows how to use IUnknown based COM Automation objects.
  4. ;
  5. ; Note: some unneeded interface functions which had special types have been replaced
  6. ; by dummies to avoid having to define those types. Do not remove these dummies as
  7. ; that would change the function indices which is bad. Also, not all function
  8. ; prototypes have been tested, only those used by this example.
  9. [Setup]
  10. AppName=My Program
  11. AppVersion=1.5
  12. WizardStyle=modern dynamic
  13. DisableWelcomePage=no
  14. CreateAppDir=no
  15. Uninstallable=no
  16. DisableProgramGroupPage=yes
  17. DefaultGroupName=My Program
  18. OutputDir=userdocs:Inno Setup Examples Output
  19. [Code]
  20. {--- IShellLink ---}
  21. const
  22. CLSID_ShellLink = '{00021401-0000-0000-C000-000000000046}';
  23. type
  24. IShellLinkW = interface(IUnknown)
  25. '{000214F9-0000-0000-C000-000000000046}'
  26. procedure Dummy;
  27. procedure Dummy2;
  28. procedure Dummy3;
  29. procedure GetDescription(pszName: String; cchMaxName: Integer); safecall;
  30. procedure SetDescription(pszName: String); safecall;
  31. procedure GetWorkingDirectory(pszDir: String; cchMaxPath: Integer); safecall;
  32. procedure SetWorkingDirectory(pszDir: String); safecall;
  33. procedure GetArguments(pszArgs: String; cchMaxPath: Integer); safecall;
  34. procedure SetArguments(pszArgs: String); safecall;
  35. function GetHotkey: Word; safecall;
  36. procedure SetHotkey(wHotkey: Word); safecall;
  37. function GetShowCmd: Integer; safecall;
  38. procedure SetShowCmd(iShowCmd: Integer); safecall;
  39. procedure GetIconLocation(pszIconPath: String; cchIconPath: Integer;
  40. out piIcon: Integer); safecall;
  41. procedure SetIconLocation(pszIconPath: String; iIcon: Integer); safecall;
  42. procedure SetRelativePath(pszPathRel: String; dwReserved: DWORD); safecall;
  43. procedure Resolve(Wnd: HWND; fFlags: DWORD); safecall;
  44. procedure SetPath(pszFile: String); safecall;
  45. end;
  46. IPersist = interface(IUnknown)
  47. '{0000010C-0000-0000-C000-000000000046}'
  48. procedure GetClassID(var classID: TGUID); safecall;
  49. end;
  50. IPersistFile = interface(IPersist)
  51. '{0000010B-0000-0000-C000-000000000046}'
  52. procedure IsDirty; safecall;
  53. procedure Load(pszFileName: String; dwMode: Longint); safecall;
  54. procedure Save(pszFileName: String; fRemember: BOOL); safecall;
  55. procedure SaveCompleted(pszFileName: String); safecall;
  56. function GetCurFile: String; safecall;
  57. end;
  58. procedure IShellLinkButtonOnClick(Sender: TObject);
  59. var
  60. Obj: IUnknown;
  61. SL: IShellLinkW;
  62. PF: IPersistFile;
  63. begin
  64. { Create the main ShellLink COM Automation object }
  65. Obj := CreateComObject(StringToGuid(CLSID_ShellLink));
  66. { Set the shortcut properties }
  67. SL := IShellLinkW(Obj);
  68. SL.SetPath(ExpandConstant('{srcexe}'));
  69. SL.SetArguments('');
  70. SL.SetShowCmd(SW_SHOWNORMAL);
  71. { Save the shortcut }
  72. PF := IPersistFile(Obj);
  73. PF.Save(ExpandConstant('{autodesktop}\CodeAutomation2 Test.lnk'), True);
  74. MsgBox('Saved a shortcut named ''CodeAutomation2 Test'' on the desktop.', mbInformation, mb_Ok);
  75. end;
  76. {--- ITaskScheduler ---}
  77. const
  78. CLSID_TaskScheduler = '{148BD52A-A2AB-11CE-B11F-00AA00530503}';
  79. CLSID_Task = '{148BD520-A2AB-11CE-B11F-00AA00530503}';
  80. IID_Task = '{148BD524-A2AB-11CE-B11F-00AA00530503}';
  81. TASK_TIME_TRIGGER_DAILY = 1;
  82. type
  83. ITaskScheduler = interface(IUnknown)
  84. '{148BD527-A2AB-11CE-B11F-00AA00530503}'
  85. procedure SetTargetComputer(pwszComputer: String); safecall;
  86. function GetTargetComputer: String; safecall;
  87. procedure Dummy;
  88. function Activate(pwszName: String; var riid: TGUID): IUnknown; safecall;
  89. procedure Delete(pwszName: String); safecall;
  90. function NewWorkItem(pwszTaskName: String; var rclsid: TGUID; var riid: TGUID): IUnknown; safecall;
  91. procedure Dummy2;
  92. function IsOfType(pwszName: String; var riid: TGUID): HResult;
  93. end;
  94. TDaily = record
  95. DaysInterval: WORD;
  96. end;
  97. TWeekly = record
  98. WeeksInterval: WORD;
  99. rgfDaysOfTheWeek: WORD;
  100. end;
  101. TMonthlyDate = record
  102. rgfDays: DWORD;
  103. rgfMonths: WORD;
  104. end;
  105. TMonthlyDow = record
  106. wWhichWeek: WORD;
  107. rgfDaysOfTheWeek: WORD;
  108. rgfMonths: WORD;
  109. end;
  110. { ROPS doesn't support unions, replace this with the type you need and adjust padding (end size has to be 48). }
  111. TTriggerTypeUnion = record
  112. Daily: TDaily;
  113. Pad1: WORD;
  114. Pad2: WORD;
  115. Pad3: WORD;
  116. end;
  117. TTaskTrigger = record
  118. cbTriggerSize: WORD;
  119. Reserved1: WORD;
  120. wBeginYear: WORD;
  121. wBeginMonth: WORD;
  122. wBeginDay: WORD;
  123. wEndYear: WORD;
  124. wEndMonth: WORD;
  125. wEndDay: WORD;
  126. wStartHour: WORD;
  127. wStartMinute: WORD;
  128. MinutesDuration: DWORD;
  129. MinutesInterval: DWORD;
  130. rgFlags: DWORD;
  131. TriggerType: DWORD;
  132. Type_: TTriggerTypeUnion;
  133. Reserved2: WORD;
  134. wRandomMinutesInterval: WORD;
  135. end;
  136. ITaskTrigger = interface(IUnknown)
  137. '{148BD52B-A2AB-11CE-B11F-00AA00530503}'
  138. procedure SetTrigger(var pTrigger: TTaskTrigger); safecall;
  139. procedure GetTrigger(var pTrigger: TTaskTrigger); safecall;
  140. procedure GetTriggerString(var ppwszTrigger: String); safecall;
  141. end;
  142. IScheduledWorkItem = interface(IUnknown)
  143. '{A6B952F0-A4B1-11D0-997D-00AA006887EC}'
  144. function CreateTrigger(out piNewTrigger: Word): ITaskTrigger; safecall;
  145. procedure DeleteTrigger(iTrigger: Word); safecall;
  146. function GetTriggerCount: Word; safecall;
  147. function GetTrigger(iTrigger: Word): ITaskTrigger; safecall;
  148. function GetTriggerString(iTrigger: Word): String; safecall;
  149. procedure Dummy;
  150. procedure Dummy2;
  151. procedure SetIdleWait(wIdleMinutes: Word; wDeadlineMinutes: Word); safecall;
  152. procedure GetIdleWait(out pwIdleMinutes: Word; out pwDeadlineMinutes: Word); safecall;
  153. procedure Run; safecall;
  154. procedure Terminate; safecall;
  155. procedure EditWorkItem(hParent: HWND; dwReserved: DWORD); safecall;
  156. procedure Dummy3;
  157. function GetStatus: HRESULT; safecall;
  158. function GetExitCode: DWORD; safecall;
  159. procedure SetComment(pwszComment: String); safecall;
  160. function GetComment: String; safecall;
  161. procedure SetCreator(pwszCreator: String); safecall;
  162. function GetCreator: String; safecall;
  163. procedure SetWorkItemData(cbData: Word; var rgbData: Byte); safecall;
  164. procedure GetWorkItemData(out pcbData: Word; out prgbData: Byte); safecall;
  165. procedure SetErrorRetryCount(wRetryCount: Word); safecall;
  166. function GetErrorRetryCount: Word; safecall;
  167. procedure SetErrorRetryInterval(wRetryInterval: Word); safecall;
  168. function GetErrorRetryInterval: Word; safecall;
  169. procedure SetFlags(dwFlags: DWORD); safecall;
  170. function GetFlags: DWORD; safecall;
  171. procedure SetAccountInformation(pwszAccountName: String; pwszPassword: String); safecall;
  172. function GetAccountInformation: String; safecall;
  173. end;
  174. ITask = interface(IScheduledWorkItem)
  175. '{148BD524-A2AB-11CE-B11F-00AA00530503}'
  176. procedure SetApplicationName(pwszApplicationName: String); safecall;
  177. procedure GetApplicationName(out ppwszApplicationName: String); safecall;
  178. procedure SetParameters(pwszParameters: String); safecall;
  179. procedure GetParameters(out ppwszParameters: String); safecall;
  180. procedure SetWorkingDirectory(pwszWorkingDirectory: String); safecall;
  181. procedure GetWorkingDirectory(out ppwszWorkingDirectory: String); safecall;
  182. procedure SetPriority(dwPriority: DWORD); safecall;
  183. procedure GetPriority(out pdwPriority: DWORD); safecall;
  184. procedure SetTaskFlags(dwFlags: DWORD); safecall;
  185. procedure GetTaskFlags(out pdwFlags: DWORD); safecall;
  186. procedure SetMaxRunTime(dwMaxRunTimeMS: DWORD); safecall;
  187. procedure GetMaxRunTime(out pdwMaxRunTimeMS: DWORD); safecall;
  188. end;
  189. procedure ITaskSchedulerButtonOnClick(Sender: TObject);
  190. var
  191. Obj, Obj2: IUnknown;
  192. TaskScheduler: ITaskScheduler;
  193. G1, G2: TGUID;
  194. Task: ITask;
  195. iNewTrigger: WORD;
  196. TaskTrigger: ITaskTrigger;
  197. TaskTrigger2: TTaskTrigger;
  198. PF: IPersistFile;
  199. begin
  200. { Create the main TaskScheduler COM Automation object }
  201. Obj := CreateComObject(StringToGuid(CLSID_TaskScheduler));
  202. { Create the Task COM automation object }
  203. TaskScheduler := ITaskScheduler(Obj);
  204. G1 := StringToGuid(CLSID_Task);
  205. G2 := StringToGuid(IID_Task);
  206. //This will throw an exception if the task already exists
  207. Obj2 := TaskScheduler.NewWorkItem('CodeAutomation2 Test', G1, G2);
  208. { Set the task properties }
  209. Task := ITask(Obj2);
  210. Task.SetComment('CodeAutomation2 Test Comment');
  211. Task.SetApplicationName(ExpandConstant('{srcexe}'));
  212. { Set the task account information }
  213. //Uncomment the following and provide actual user info to get a runnable task
  214. //Task.SetAccountInformation('username', 'password');
  215. { Create the TaskTrigger COM automation object }
  216. TaskTrigger := Task.CreateTrigger(iNewTrigger);
  217. { Set the task trigger properties }
  218. with TaskTrigger2 do begin
  219. cbTriggerSize := SizeOf(TaskTrigger2);
  220. wBeginYear := 2009;
  221. wBeginMonth := 10;
  222. wBeginDay := 1;
  223. wStartHour := 12;
  224. TriggerType := TASK_TIME_TRIGGER_DAILY;
  225. Type_.Daily.DaysInterval := 1;
  226. end;
  227. TaskTrigger.SetTrigger(TaskTrigger2);
  228. { Save the task }
  229. PF := IPersistFile(Obj2);
  230. PF.Save('', True);
  231. MsgBox('Created a daily task named named ''CodeAutomation2 Test''.' + #13#13 + 'Note: Account information not set so the task won''t actually run, uncomment the SetAccountInfo call and provide actual user info to get a runnable task.', mbInformation, mb_Ok);
  232. end;
  233. {---}
  234. procedure CreateButton(ALeft, ATop: Integer; ACaption: String; ANotifyEvent: TNotifyEvent);
  235. begin
  236. with TNewButton.Create(WizardForm) do begin
  237. Left := ALeft;
  238. Top := ATop;
  239. Width := (WizardForm.CancelButton.Width*3)/2;
  240. Height := WizardForm.CancelButton.Height;
  241. Caption := ACaption;
  242. OnClick := ANotifyEvent;
  243. Parent := WizardForm.WelcomePage;
  244. end;
  245. end;
  246. procedure InitializeWizard();
  247. var
  248. Left, LeftInc, Top, TopInc: Integer;
  249. begin
  250. Left := WizardForm.WelcomeLabel2.Left;
  251. LeftInc := (WizardForm.CancelButton.Width*3)/2 + ScaleX(8);
  252. TopInc := WizardForm.CancelButton.Height + ScaleY(8);
  253. Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height - 4*TopInc;
  254. CreateButton(Left, Top, '&IShellLink...', @IShellLinkButtonOnClick);
  255. Top := Top + TopInc;
  256. CreateButton(Left, Top, '&ITaskScheduler...', @ITaskSchedulerButtonOnClick);
  257. end;