CodeAutomation2.iss 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. ; protoypes have been tested, only those used by this example.
  9. [Setup]
  10. AppName=My Program
  11. AppVersion=1.5
  12. WizardStyle=modern
  13. DisableWelcomePage=no
  14. CreateAppDir=no
  15. DisableProgramGroupPage=yes
  16. DefaultGroupName=My Program
  17. UninstallDisplayIcon={app}\MyProg.exe
  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. function GetDescription(pszName: String; cchMaxName: Integer): HResult;
  30. function SetDescription(pszName: String): HResult;
  31. function GetWorkingDirectory(pszDir: String; cchMaxPath: Integer): HResult;
  32. function SetWorkingDirectory(pszDir: String): HResult;
  33. function GetArguments(pszArgs: String; cchMaxPath: Integer): HResult;
  34. function SetArguments(pszArgs: String): HResult;
  35. function GetHotkey(var pwHotkey: Word): HResult;
  36. function SetHotkey(wHotkey: Word): HResult;
  37. function GetShowCmd(out piShowCmd: Integer): HResult;
  38. function SetShowCmd(iShowCmd: Integer): HResult;
  39. function GetIconLocation(pszIconPath: String; cchIconPath: Integer;
  40. out piIcon: Integer): HResult;
  41. function SetIconLocation(pszIconPath: String; iIcon: Integer): HResult;
  42. function SetRelativePath(pszPathRel: String; dwReserved: DWORD): HResult;
  43. function Resolve(Wnd: HWND; fFlags: DWORD): HResult;
  44. function SetPath(pszFile: String): HResult;
  45. end;
  46. IPersist = interface(IUnknown)
  47. '{0000010C-0000-0000-C000-000000000046}'
  48. function GetClassID(var classID: TGUID): HResult;
  49. end;
  50. IPersistFile = interface(IPersist)
  51. '{0000010B-0000-0000-C000-000000000046}'
  52. function IsDirty: HResult;
  53. function Load(pszFileName: String; dwMode: Longint): HResult;
  54. function Save(pszFileName: String; fRemember: BOOL): HResult;
  55. function SaveCompleted(pszFileName: String): HResult;
  56. function GetCurFile(out pszFileName: String): HResult;
  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. OleCheck(SL.SetPath(ExpandConstant('{srcexe}')));
  69. OleCheck(SL.SetArguments(''));
  70. OleCheck(SL.SetShowCmd(SW_SHOWNORMAL));
  71. { Save the shortcut }
  72. PF := IPersistFile(Obj);
  73. OleCheck(PF.Save(ExpandConstant('{autodesktop}\CodeAutomation2 Test.lnk'), True));
  74. MsgBox('Saved a shortcut named ''CodeAutomation2 Test'' on the common 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. function SetTargetComputer(pwszComputer: String): HResult;
  86. function GetTargetComputer(out ppwszComputer: String): HResult;
  87. procedure Dummy;
  88. function Activate(pwszName: String; var riid: TGUID; out ppUnk: IUnknown): HResult;
  89. function Delete(pwszName: String): HResult;
  90. function NewWorkItem(pwszTaskName: String; var rclsid: TGUID; var riid: TGUID; out ppUnk: IUnknown): HResult;
  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. TMonthyDate = 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. function SetTrigger(var pTrigger: TTaskTrigger): HResult;
  139. function GetTrigger(var pTrigger: TTaskTrigger): HResult;
  140. function GetTriggerString(var ppwszTrigger: String): HResult;
  141. end;
  142. IScheduledWorkItem = interface(IUnknown)
  143. '{A6B952F0-A4B1-11D0-997D-00AA006887EC}'
  144. function CreateTrigger(out piNewTrigger: Word; out ppTrigger: ITaskTrigger): HResult;
  145. function DeleteTrigger(iTrigger: Word): HResult;
  146. function GetTriggerCount(out pwCount: Word): HResult;
  147. function GetTrigger(iTrigger: Word; var ppTrigger: ITaskTrigger): HResult;
  148. function GetTriggerString(iTrigger: Word; out ppwszTrigger: String): HResult;
  149. procedure Dummy;
  150. procedure Dummy2;
  151. function SetIdleWait(wIdleMinutes: Word; wDeadlineMinutes: Word): HResult;
  152. function GetIdleWait(out pwIdleMinutes: Word; out pwDeadlineMinutes: Word): HResult;
  153. function Run: HResult;
  154. function Terminate: HResult;
  155. function EditWorkItem(hParent: HWND; dwReserved: DWORD): HResult;
  156. procedure Dummy3;
  157. function GetStatus(out phrStatus: HResult): HResult;
  158. function GetExitCode(out pdwExitCode: DWORD): HResult;
  159. function SetComment(pwszComment: String): HResult;
  160. function GetComment(out ppwszComment: String): HResult;
  161. function SetCreator(pwszCreator: String): HResult;
  162. function GetCreator(out ppwszCreator: String): HResult;
  163. function SetWorkItemData(cbData: Word; var rgbData: Byte): HResult;
  164. function GetWorkItemData(out pcbData: Word; out prgbData: Byte): HResult;
  165. function SetErrorRetryCount(wRetryCount: Word): HResult;
  166. function GetErrorRetryCount(out pwRetryCount: Word): HResult;
  167. function SetErrorRetryInterval(wRetryInterval: Word): HResult;
  168. function GetErrorRetryInterval(out pwRetryInterval: Word): HResult;
  169. function SetFlags(dwFlags: DWORD): HResult;
  170. function GetFlags(out pdwFlags: DWORD): HResult;
  171. function SetAccountInformation(pwszAccountName: String; pwszPassword: String): HResult;
  172. function GetAccountInformation(out ppwszAccountName: String): HResult;
  173. end;
  174. ITask = interface(IScheduledWorkItem)
  175. '{148BD524-A2AB-11CE-B11F-00AA00530503}'
  176. function SetApplicationName(pwszApplicationName: String): HResult;
  177. function GetApplicationName(out ppwszApplicationName: String): HResult;
  178. function SetParameters(pwszParameters: String): HResult;
  179. function GetParameters(out ppwszParameters: String): HResult;
  180. function SetWorkingDirectory(pwszWorkingDirectory: String): HResult;
  181. function GetWorkingDirectory(out ppwszWorkingDirectory: String): HResult;
  182. function SetPriority(dwPriority: DWORD): HResult;
  183. function GetPriority(out pdwPriority: DWORD): HResult;
  184. function SetTaskFlags(dwFlags: DWORD): HResult;
  185. function GetTaskFlags(out pdwFlags: DWORD): HResult;
  186. function SetMaxRunTime(dwMaxRunTimeMS: DWORD): HResult;
  187. function GetMaxRunTime(out pdwMaxRunTimeMS: DWORD): HResult;
  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. OleCheck(TaskScheduler.NewWorkItem('CodeAutomation2 Test', G1, G2, Obj2));
  208. { Set the task properties }
  209. Task := ITask(Obj2);
  210. OleCheck(Task.SetComment('CodeAutomation2 Test Comment'));
  211. OleCheck(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. //OleCheck(Task.SetAccountInformation('username', 'password'));
  215. { Create the TaskTrigger COM automation object }
  216. OleCheck(Task.CreateTrigger(iNewTrigger, TaskTrigger));
  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. OleCheck(TaskTrigger.SetTrigger(TaskTrigger2));
  228. { Save the task }
  229. PF := IPersistFile(Obj2);
  230. OleCheck(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 TButton.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;