appinstall.iss 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "service.iss"
  2. #define SERVICE_NAME "DnsService"
  3. #define SERVICE_FILE "DnsService.exe"
  4. #define SERVICE_DISPLAY_NAME "Technitium DNS Server"
  5. #define SERVICE_DESCRIPTION "Technitium DNS Server"
  6. #define TRAYAPP_FILENAME "DnsServerSystemTrayApp.exe"
  7. [Code]
  8. {
  9. Kills the tray app
  10. }
  11. procedure KillTrayApp;
  12. begin
  13. TaskKill('{#TRAYAPP_FILENAME}');
  14. end;
  15. {
  16. Stops the service
  17. }
  18. procedure DoStopService();
  19. var
  20. stopCounter: Integer;
  21. begin
  22. stopCounter := 0;
  23. if IsServiceInstalled('{#SERVICE_NAME}') then begin
  24. Log('Service: Already installed');
  25. if IsServiceRunning('{#SERVICE_NAME}') then begin
  26. Log('Service: Already running, stopping service...');
  27. StopService('{#SERVICE_NAME}');
  28. while IsServiceRunning('{#SERVICE_NAME}') do
  29. begin
  30. if stopCounter > 2 then begin
  31. Log('Service: Waited too long to stop, killing task...');
  32. TaskKill('{#SERVICE_FILE}');
  33. Log('Service: Task killed');
  34. break;
  35. end else begin
  36. Log('Service: Waiting for stop');
  37. Sleep(2000);
  38. stopCounter := stopCounter + 1
  39. end;
  40. end;
  41. if stopCounter < 3 then Log('Service: Stopped');
  42. end;
  43. end;
  44. end;
  45. {
  46. Removes the service from the computer
  47. }
  48. procedure DoRemoveService();
  49. var
  50. stopCounter: Integer;
  51. begin
  52. stopCounter := 0;
  53. if IsServiceInstalled('{#SERVICE_NAME}') then begin
  54. Log('Service: Already installed, begin remove...');
  55. if IsServiceRunning('{#SERVICE_NAME}') then begin
  56. Log('Service: Already running, stopping...');
  57. StopService('{#SERVICE_NAME}');
  58. while IsServiceRunning('{#SERVICE_NAME}') do
  59. begin
  60. if stopCounter > 2 then begin
  61. Log('Service: Waited too long to stop, killing task...');
  62. TaskKill('{#SERVICE_FILE}');
  63. Log('Service: Task killed');
  64. break;
  65. end else begin
  66. Log('Service: Waiting for stop');
  67. Sleep(5000);
  68. stopCounter := stopCounter + 1
  69. end;
  70. end;
  71. end;
  72. stopCounter := 0;
  73. Log('Service: Removing...');
  74. RemoveService('{#SERVICE_NAME}');
  75. while IsServiceInstalled('{#SERVICE_NAME}') do
  76. begin
  77. if stopCounter > 2 then begin
  78. Log('Service: Waited too long to remove, continuing');
  79. break;
  80. end else begin
  81. Log('Service: Waiting for removal');
  82. Sleep(5000);
  83. stopCounter := stopCounter + 1
  84. end;
  85. end;
  86. if stopCounter < 3 then Log('Service: Removed');
  87. end;
  88. end;
  89. {
  90. Installs the service onto the computer
  91. }
  92. procedure DoInstallService();
  93. var
  94. InstallSuccess: Boolean;
  95. stopCounter: Integer;
  96. begin
  97. stopCounter := 0;
  98. if IsServiceInstalled('{#SERVICE_NAME}') then begin
  99. Log('Service: Already installed, skip install service');
  100. end else begin
  101. Log('Service: Begin Install');
  102. InstallSuccess := InstallService(ExpandConstant('{app}\DnsService.exe'), '{#SERVICE_NAME}', '{#SERVICE_DISPLAY_NAME}', '{#SERVICE_DESCRIPTION}', SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START);
  103. if not InstallSuccess then
  104. begin
  105. Log('Service: Install Fail ' + ServiceErrorToMessage(GetLastError()));
  106. SuppressibleMsgBox(ExpandConstant('{cm:ServiceInstallFailure,' + ServiceErrorToMessage(GetLastError()) + '}'), mbCriticalError, MB_OK, IDOK);
  107. end else begin
  108. Log('Service: Install Success, Starting...');
  109. StartService('{#SERVICE_NAME}');
  110. while IsServiceRunning('{#SERVICE_NAME}') <> true do
  111. begin
  112. if stopCounter > 3 then begin
  113. Log('Service: Waited too long to start, continue');
  114. break;
  115. end else begin
  116. Log('Service: still starting')
  117. Sleep(5000);
  118. stopCounter := stopCounter + 1
  119. end;
  120. end;
  121. if stopCounter < 4 then Log('Service: Started');
  122. end;
  123. end;
  124. end;