SimpleSC.dpr 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. library SimpleSC;
  2. uses
  3. Winapi.Windows,
  4. System.SysUtils,
  5. NSIS in 'NSIS.pas',
  6. ServiceControl in 'ServiceControl.pas',
  7. LSASecurityControl in 'LSASecurityControl.pas';
  8. function BoolToStr(Value: Boolean): String;
  9. begin
  10. if Value then
  11. Result := '1'
  12. else
  13. Result := '0';
  14. end;
  15. function StrToBool(Value: String): Boolean;
  16. begin
  17. Result := Value = '1';
  18. end;
  19. procedure InstallService(const hwndParent: HWND; const string_size: integer;
  20. const variables: PChar; const stacktop: pointer); cdecl;
  21. var
  22. ServiceName: String;
  23. DisplayName: String;
  24. ServiceType: Cardinal;
  25. StartType: Cardinal;
  26. BinaryPath: String;
  27. Dependencies: String;
  28. Username: String;
  29. Password: String;
  30. ServiceResult: String;
  31. begin
  32. Init(hwndParent, string_size, variables, stacktop);
  33. ServiceName := PopString;
  34. DisplayName := PopString;
  35. ServiceType := StrToInt(PopString);
  36. StartType := StrToInt(PopString);
  37. BinaryPath := PopString;
  38. Dependencies := PopString;
  39. Username := PopString;
  40. Password := PopString;
  41. ServiceResult := IntToStr(ServiceControl.InstallService(ServiceName, DisplayName, ServiceType, StartType, BinaryPath, Dependencies, Username, Password));
  42. PushString(ServiceResult);
  43. end;
  44. procedure RemoveService(const hwndParent: HWND; const string_size: integer;
  45. const variables: PChar; const stacktop: pointer); cdecl;
  46. var
  47. ServiceName: String;
  48. ServiceResult: String;
  49. begin
  50. Init(hwndParent, string_size, variables, stacktop);
  51. ServiceName := PopString;
  52. ServiceResult := IntToStr(ServiceControl.RemoveService(ServiceName));
  53. PushString(ServiceResult);
  54. end;
  55. procedure StartService(const hwndParent: HWND; const string_size: integer;
  56. const variables: PChar; const stacktop: pointer); cdecl;
  57. var
  58. ServiceName: String;
  59. ServiceArguments: String;
  60. Timeout: Integer;
  61. ServiceResult: String;
  62. begin
  63. Init(hwndParent, string_size, variables, stacktop);
  64. ServiceName := PopString;
  65. ServiceArguments := PopString;
  66. Timeout := StrToInt(PopString);
  67. ServiceResult := IntToStr(ServiceControl.StartService(ServiceName, ServiceArguments, Timeout));
  68. PushString(ServiceResult);
  69. end;
  70. procedure StopService(const hwndParent: HWND; const string_size: integer;
  71. const variables: PChar; const stacktop: pointer); cdecl;
  72. var
  73. ServiceName: String;
  74. WaitForFileRelease: Boolean;
  75. Timeout: Integer;
  76. ServiceResult: String;
  77. begin
  78. Init(hwndParent, string_size, variables, stacktop);
  79. ServiceName := PopString;
  80. WaitForFileRelease := StrToBool(PopString);
  81. Timeout := StrToInt(PopString);
  82. ServiceResult := IntToStr(ServiceControl.StopService(ServiceName, WaitForFileRelease, Timeout));
  83. PushString(ServiceResult);
  84. end;
  85. procedure PauseService(const hwndParent: HWND; const string_size: integer;
  86. const variables: PChar; const stacktop: pointer); cdecl;
  87. var
  88. ServiceName: String;
  89. Timeout: Integer;
  90. ServiceResult: String;
  91. begin
  92. Init(hwndParent, string_size, variables, stacktop);
  93. ServiceName := PopString;
  94. Timeout := StrToInt(PopString);
  95. ServiceResult := IntToStr(ServiceControl.PauseService(ServiceName, Timeout));
  96. PushString(ServiceResult)
  97. end;
  98. procedure ContinueService(const hwndParent: HWND; const string_size: integer;
  99. const variables: PChar; const stacktop: pointer); cdecl;
  100. var
  101. ServiceName: String;
  102. Timeout: Integer;
  103. ServiceResult: String;
  104. begin
  105. Init(hwndParent, string_size, variables, stacktop);
  106. ServiceName := PopString;
  107. Timeout := StrToInt(PopString);
  108. ServiceResult := IntToStr(ServiceControl.ContinueService(ServiceName, Timeout));
  109. PushString(ServiceResult)
  110. end;
  111. procedure GetServiceName(const hwndParent: HWND; const string_size: integer;
  112. const variables: PChar; const stacktop: pointer); cdecl;
  113. Var
  114. DisplayName: String;
  115. ServiceResult: String;
  116. ServiceName: String;
  117. begin
  118. Init(hwndParent, string_size, variables, stacktop);
  119. DisplayName := PopString;
  120. ServiceResult := IntToStr(ServiceControl.GetServiceName(DisplayName, ServiceName));
  121. PushString(ServiceName);
  122. PushString(ServiceResult);
  123. end;
  124. procedure GetServiceDisplayName(const hwndParent: HWND; const string_size: integer;
  125. const variables: PChar; const stacktop: pointer); cdecl;
  126. Var
  127. ServiceName: String;
  128. DisplayName: String;
  129. ServiceResult: String;
  130. begin
  131. Init(hwndParent, string_size, variables, stacktop);
  132. ServiceName := PopString;
  133. ServiceResult := IntToStr(ServiceControl.GetServiceDisplayName(ServiceName, DisplayName));
  134. PushString(DisplayName);
  135. PushString(ServiceResult);
  136. end;
  137. procedure GetServiceStatus(const hwndParent: HWND; const string_size: integer;
  138. const variables: PChar; const stacktop: pointer); cdecl;
  139. var
  140. ServiceName: String;
  141. Status: DWORD;
  142. ServiceResult: String;
  143. begin
  144. Init(hwndParent, string_size, variables, stacktop);
  145. ServiceName := PopString;
  146. ServiceResult := IntToStr(ServiceControl.GetServiceStatus(ServiceName, Status));
  147. PushString(IntToStr(Status));
  148. PushString(ServiceResult);
  149. end;
  150. procedure GetServiceBinaryPath(const hwndParent: HWND; const string_size: integer;
  151. const variables: PChar; const stacktop: pointer); cdecl;
  152. var
  153. ServiceName: String;
  154. BinaryPath: String;
  155. ServiceResult: String;
  156. begin
  157. Init(hwndParent, string_size, variables, stacktop);
  158. ServiceName := PopString;
  159. ServiceResult := IntToStr(ServiceControl.GetServiceBinaryPath(ServiceName, BinaryPath));
  160. PushString(BinaryPath);
  161. PushString(ServiceResult);
  162. end;
  163. procedure GetServiceDescription(const hwndParent: HWND; const string_size: integer;
  164. const variables: PChar; const stacktop: pointer); cdecl;
  165. var
  166. ServiceName: String;
  167. Description: String;
  168. ServiceResult: String;
  169. begin
  170. Init(hwndParent, string_size, variables, stacktop);
  171. ServiceName := PopString;
  172. ServiceResult := IntToStr(ServiceControl.GetServiceDescription(ServiceName, Description));
  173. PushString(Description);
  174. PushString(ServiceResult);
  175. end;
  176. procedure GetServiceStartType(const hwndParent: HWND; const string_size: integer;
  177. const variables: PChar; const stacktop: pointer); cdecl;
  178. var
  179. ServiceName: String;
  180. StartType: DWORD;
  181. ServiceResult: String;
  182. begin
  183. Init(hwndParent, string_size, variables, stacktop);
  184. ServiceName := PopString;
  185. ServiceResult := IntToStr(ServiceControl.GetServiceStartType(ServiceName, StartType));
  186. PushString(IntToStr(StartType));
  187. PushString(ServiceResult);
  188. end;
  189. procedure GetServiceLogon(const hwndParent: HWND; const string_size: integer;
  190. const variables: PChar; const stacktop: pointer); cdecl;
  191. var
  192. ServiceName: String;
  193. Username: String;
  194. ServiceResult: String;
  195. begin
  196. Init(hwndParent, string_size, variables, stacktop);
  197. ServiceName := PopString;
  198. ServiceResult := IntToStr(ServiceControl.GetServiceLogon(ServiceName, Username));
  199. PushString(Username);
  200. PushString(ServiceResult);
  201. end;
  202. procedure GetServiceFailure(const hwndParent: HWND; const string_size: integer;
  203. const variables: PChar; const stacktop: pointer); cdecl;
  204. var
  205. ServiceName: String;
  206. ResetPeriod: DWORD;
  207. RebootMessage: String;
  208. Command: String;
  209. Action1: Integer;
  210. ActionDelay1: DWORD;
  211. Action2: Integer;
  212. ActionDelay2: DWORD;
  213. Action3: Integer;
  214. ActionDelay3: DWORD;
  215. ServiceResult: String;
  216. begin
  217. Init(hwndParent, string_size, variables, stacktop);
  218. ServiceName := PopString;
  219. ServiceResult := IntToStr(ServiceControl.GetServiceFailure(ServiceName, ResetPeriod, RebootMessage, Command, Action1, ActionDelay1, Action2, ActionDelay2, Action3, ActionDelay3));
  220. PushString(IntToStr(ActionDelay3));
  221. PushString(IntToStr(Action3));
  222. PushString(IntToStr(ActionDelay2));
  223. PushString(IntToStr(Action2));
  224. PushString(IntToStr(ActionDelay1));
  225. PushString(IntToStr(Action1));
  226. PushString(Command);
  227. PushString(RebootMessage);
  228. PushString(IntToStr(ResetPeriod));
  229. PushString(ServiceResult);
  230. end;
  231. procedure GetServiceFailureFlag(const hwndParent: HWND; const string_size: integer;
  232. const variables: PChar; const stacktop: pointer); cdecl;
  233. var
  234. ServiceName: String;
  235. FailureActionsOnNonCrashFailures: Boolean;
  236. ServiceResult: String;
  237. begin
  238. Init(hwndParent, string_size, variables, stacktop);
  239. ServiceName := PopString;
  240. ServiceResult := IntToStr(ServiceControl.GetServiceFailureFlag(ServiceName, FailureActionsOnNonCrashFailures));
  241. PushString(BoolToStr(FailureActionsOnNonCrashFailures));
  242. PushString(ServiceResult);
  243. end;
  244. procedure GetServiceDelayedAutoStartInfo(const hwndParent: HWND; const string_size: integer;
  245. const variables: PChar; const stacktop: pointer); cdecl;
  246. var
  247. ServiceName: String;
  248. DelayedAutostart: Boolean;
  249. ServiceResult: String;
  250. begin
  251. Init(hwndParent, string_size, variables, stacktop);
  252. ServiceName := PopString;
  253. ServiceResult := IntToStr(ServiceControl.GetServiceDelayedAutoStartInfo(ServiceName, DelayedAutostart));
  254. PushString(BoolToStr(DelayedAutostart));
  255. PushString(ServiceResult);
  256. end;
  257. procedure SetServiceDescription(const hwndParent: HWND; const string_size: integer;
  258. const variables: PChar; const stacktop: pointer); cdecl;
  259. var
  260. ServiceName: String;
  261. Description: String;
  262. ServiceResult: String;
  263. begin
  264. Init(hwndParent, string_size, variables, stacktop);
  265. ServiceName := PopString;
  266. Description := PopString;
  267. ServiceResult := IntToStr(ServiceControl.SetServiceDescription(ServiceName, Description));
  268. PushString(ServiceResult);
  269. end;
  270. procedure SetServiceStartType(const hwndParent: HWND; const string_size: integer;
  271. const variables: PChar; const stacktop: pointer); cdecl;
  272. var
  273. ServiceName: String;
  274. ServiceStartType: DWORD;
  275. ServiceResult: String;
  276. begin
  277. Init(hwndParent, string_size, variables, stacktop);
  278. ServiceName := PopString;
  279. ServiceStartType := StrToInt(PopString);
  280. ServiceResult := IntToStr(ServiceControl.SetServiceStartType(ServiceName, ServiceStartType));
  281. PushString(ServiceResult);
  282. end;
  283. procedure SetServiceLogon(const hwndParent: HWND; const string_size: integer;
  284. const variables: PChar; const stacktop: pointer); cdecl;
  285. var
  286. ServiceName: String;
  287. Username: String;
  288. Password: String;
  289. ServiceResult: String;
  290. begin
  291. Init(hwndParent, string_size, variables, stacktop);
  292. ServiceName := PopString;
  293. Username := PopString;
  294. Password := PopString;
  295. ServiceResult := IntToStr(ServiceControl.SetServiceLogon(ServiceName, Username, Password));
  296. PushString(ServiceResult);
  297. end;
  298. procedure SetServiceBinaryPath(const hwndParent: HWND; const string_size: integer;
  299. const variables: PChar; const stacktop: pointer); cdecl;
  300. var
  301. ServiceName: String;
  302. BinaryPath: String;
  303. ServiceResult: String;
  304. begin
  305. Init(hwndParent, string_size, variables, stacktop);
  306. ServiceName := PopString;
  307. BinaryPath := PopString;
  308. ServiceResult := IntToStr(ServiceControl.SetServiceBinaryPath(ServiceName, BinaryPath));
  309. PushString(ServiceResult);
  310. end;
  311. procedure SetServiceFailure(const hwndParent: HWND; const string_size: integer;
  312. const variables: PChar; const stacktop: pointer); cdecl;
  313. var
  314. ServiceName: String;
  315. ResetPeriod: DWORD;
  316. RebootMessage: String;
  317. Command: String;
  318. Action1: Integer;
  319. ActionDelay1: DWORD;
  320. Action2: Integer;
  321. ActionDelay2: DWORD;
  322. Action3: Integer;
  323. ActionDelay3: DWORD;
  324. ServiceResult: Integer;
  325. PrivilegeResult: Integer;
  326. const
  327. SE_SHUTDOWN_PRIVILEGE = 'SeShutdownPrivilege';
  328. SC_ACTION_REBOOT = 2;
  329. begin
  330. Init(hwndParent, string_size, variables, stacktop);
  331. ServiceName := PopString;
  332. ResetPeriod := StrToInt(PopString);
  333. RebootMessage := PopString;
  334. Command := PopString;
  335. Action1 := StrToInt(PopString);
  336. ActionDelay1 := StrToInt(PopString);
  337. Action2 := StrToInt(PopString);
  338. ActionDelay2 := StrToInt(PopString);
  339. Action3 := StrToInt(PopString);
  340. ActionDelay3 := StrToInt(PopString);
  341. if (Action1 = SC_ACTION_REBOOT) or (Action2 = SC_ACTION_REBOOT) or (Action3 = SC_ACTION_REBOOT) then
  342. begin
  343. PrivilegeResult := LSASecurityControl.EnablePrivilege(SE_SHUTDOWN_PRIVILEGE);
  344. if not PrivilegeResult = 0 then
  345. begin
  346. PushString(IntToStr(PrivilegeResult));
  347. Exit;
  348. end;
  349. end;
  350. ServiceResult := ServiceControl.SetServiceFailure(ServiceName, ResetPeriod, RebootMessage, Command, Action1, ActionDelay1,
  351. Action2, ActionDelay2, Action3, ActionDelay3);
  352. if (Action1 = SC_ACTION_REBOOT) or (Action2 = SC_ACTION_REBOOT) or (Action3 = SC_ACTION_REBOOT) then
  353. begin
  354. PrivilegeResult := LSASecurityControl.DisablePrivilege(SE_SHUTDOWN_PRIVILEGE);
  355. if not PrivilegeResult = 0 then
  356. begin
  357. PushString(IntToStr(PrivilegeResult));
  358. Exit;
  359. end;
  360. end;
  361. PushString(IntToStr(ServiceResult));
  362. end;
  363. procedure SetServiceFailureFlag(const hwndParent: HWND; const string_size: integer;
  364. const variables: PChar; const stacktop: pointer); cdecl;
  365. var
  366. ServiceName: String;
  367. FailureActionsOnNonCrashFailures: Boolean;
  368. ServiceResult: String;
  369. begin
  370. Init(hwndParent, string_size, variables, stacktop);
  371. ServiceName := PopString;
  372. FailureActionsOnNonCrashFailures := StrToBool(PopString);
  373. ServiceResult := IntToStr(ServiceControl.SetServiceFailureFlag(ServiceName, FailureActionsOnNonCrashFailures));
  374. PushString(ServiceResult)
  375. end;
  376. procedure SetServiceDelayedAutoStartInfo(const hwndParent: HWND; const string_size: integer;
  377. const variables: PChar; const stacktop: pointer); cdecl;
  378. var
  379. ServiceName: String;
  380. DelayedAutostart: Boolean;
  381. ServiceResult: String;
  382. begin
  383. Init(hwndParent, string_size, variables, stacktop);
  384. ServiceName := PopString;
  385. DelayedAutostart := StrToBool(PopString);
  386. ServiceResult := IntToStr(ServiceControl.SetServiceDelayedAutoStartInfo(ServiceName, DelayedAutostart));
  387. PushString(ServiceResult)
  388. end;
  389. procedure ServiceIsRunning(const hwndParent: HWND; const string_size: integer;
  390. const variables: PChar; const stacktop: pointer); cdecl;
  391. var
  392. ServiceName: String;
  393. IsRunning: Boolean;
  394. ServiceResult: String;
  395. begin
  396. Init(hwndParent, string_size, variables, stacktop);
  397. ServiceName := PopString;
  398. ServiceResult := IntToStr(ServiceControl.ServiceIsRunning(ServiceName, IsRunning));
  399. PushString(BoolToStr(IsRunning));
  400. PushString(ServiceResult);
  401. end;
  402. procedure ServiceIsStopped(const hwndParent: HWND; const string_size: integer;
  403. const variables: PChar; const stacktop: pointer); cdecl;
  404. var
  405. ServiceName: String;
  406. IsStopped: Boolean;
  407. ServiceResult: String;
  408. begin
  409. Init(hwndParent, string_size, variables, stacktop);
  410. ServiceName := PopString;
  411. ServiceResult := IntToStr(ServiceControl.ServiceIsStopped(ServiceName, IsStopped));
  412. PushString(BoolToStr(IsStopped));
  413. PushString(ServiceResult);
  414. end;
  415. procedure ServiceIsPaused(const hwndParent: HWND; const string_size: integer;
  416. const variables: PChar; const stacktop: pointer); cdecl;
  417. var
  418. ServiceName: String;
  419. IsPaused: Boolean;
  420. ServiceResult: String;
  421. begin
  422. Init(hwndParent, string_size, variables, stacktop);
  423. ServiceName := PopString;
  424. ServiceResult := IntToStr(ServiceControl.ServiceIsPaused(ServiceName, IsPaused));
  425. PushString(BoolToStr(IsPaused));
  426. PushString(ServiceResult);
  427. end;
  428. procedure RestartService(const hwndParent: HWND; const string_size: integer;
  429. const variables: PChar; const stacktop: pointer); cdecl;
  430. var
  431. ServiceName: String;
  432. ServiceArguments: String;
  433. Timeout: Integer;
  434. ServiceResult: String;
  435. begin
  436. Init(hwndParent, string_size, variables, stacktop);
  437. ServiceName := PopString;
  438. ServiceArguments := PopString;
  439. Timeout := StrToInt(PopString);
  440. ServiceResult := IntToStr(ServiceControl.RestartService(ServiceName, ServiceArguments, Timeout));
  441. PushString(ServiceResult);
  442. end;
  443. procedure ExistsService(const hwndParent: HWND; const string_size: integer;
  444. const variables: PChar; const stacktop: pointer); cdecl;
  445. var
  446. ServiceName: String;
  447. ServiceResult: String;
  448. begin
  449. Init(hwndParent, string_size, variables, stacktop);
  450. ServiceName := PopString;
  451. ServiceResult := IntToStr(ServiceControl.ExistsService(ServiceName));
  452. PushString(ServiceResult);
  453. end;
  454. procedure GrantServiceLogonPrivilege(const hwndParent: HWND; const string_size: integer;
  455. const variables: PChar; const stacktop: pointer); cdecl;
  456. var
  457. AccountName: String;
  458. LSAResult: String;
  459. const
  460. SE_SERVICE_LOGON_RIGHT = 'SeServiceLogonRight';
  461. begin
  462. Init(hwndParent, string_size, variables, stacktop);
  463. AccountName := PopString;
  464. LSAResult := IntToStr(LSASecurityControl.GrantPrivilege(AccountName, SE_SERVICE_LOGON_RIGHT));
  465. PushString(LSAResult);
  466. end;
  467. procedure RemoveServiceLogonPrivilege(const hwndParent: HWND; const string_size: integer;
  468. const variables: PChar; const stacktop: pointer); cdecl;
  469. var
  470. AccountName: String;
  471. LSAResult: String;
  472. const
  473. SE_SERVICE_LOGON_RIGHT = 'SeServiceLogonRight';
  474. begin
  475. Init(hwndParent, string_size, variables, stacktop);
  476. AccountName := PopString;
  477. LSAResult := IntToStr(LSASecurityControl.RemovePrivilege(AccountName, SE_SERVICE_LOGON_RIGHT));
  478. PushString(LSAResult);
  479. end;
  480. procedure GetErrorMessage(const hwndParent: HWND; const string_size: integer;
  481. const variables: PChar; const stacktop: pointer); cdecl;
  482. var
  483. ErrorCode: Integer;
  484. ErrorMessage: String;
  485. begin
  486. Init(hwndParent, string_size, variables, stacktop);
  487. ErrorCode := StrToInt(PopString);
  488. ErrorMessage := ServiceControl.GetErrorMessage(ErrorCode);
  489. PushString(ErrorMessage);
  490. end;
  491. exports InstallService;
  492. exports ExistsService;
  493. exports RemoveService;
  494. exports StartService;
  495. exports StopService;
  496. exports PauseService;
  497. exports ContinueService;
  498. exports GetServiceName;
  499. exports GetServiceDisplayName;
  500. exports GetServiceStatus;
  501. exports GetServiceBinaryPath;
  502. exports GetServiceDescription;
  503. exports GetServiceStartType;
  504. exports GetServiceLogon;
  505. exports GetServiceFailure;
  506. exports GetServiceFailureFlag;
  507. exports GetServiceDelayedAutoStartInfo;
  508. exports SetServiceDescription;
  509. exports SetServiceStartType;
  510. exports SetServiceLogon;
  511. exports SetServiceBinaryPath;
  512. exports SetServiceFailure;
  513. exports SetServiceFailureFlag;
  514. exports SetServiceDelayedAutoStartInfo;
  515. exports ServiceIsRunning;
  516. exports ServiceIsStopped;
  517. exports ServiceIsPaused;
  518. exports RestartService;
  519. exports GrantServiceLogonPrivilege;
  520. exports RemoveServiceLogonPrivilege;
  521. exports GetErrorMessage;
  522. end.