appinstall.iss 3.9 KB

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