daily pastebin goal
89%
SHARE
TWEET

Servicess_Full.iss

a guest Dec 18th, 2017 386 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;* Original source: http://goo.gl/PTi56
  2. ;*
  3. ;* Process Hacker - Various services functions
  4. ;*
  5. ;* This file is part of Process Hacker.
  6. ;*
  7. ;* Process Hacker is free software; you can redistribute it and/or modify
  8. ;* it under the terms of the GNU General Public License as published by
  9. ;* the Free Software Foundation, either version 3 of the License, or
  10. ;* (at your option) any later version.
  11. ;*
  12. ;* Process Hacker is distributed in the hope that it will be useful,
  13. ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;* GNU General Public License for more details.
  16. ;*
  17. ;* You should have received a copy of the GNU General Public License
  18. ;* along with Process Hacker.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.  
  21. [Code]
  22. // Various service related functions
  23.  
  24. type
  25.   SERVICE_STATUS = record
  26.     dwServiceType             : cardinal;
  27.     dwCurrentState            : cardinal;
  28.     dwControlsAccepted        : cardinal;
  29.     dwWin32ExitCode           : cardinal;
  30.     dwServiceSpecificExitCode : cardinal;
  31.     dwCheckPoint              : cardinal;
  32.     dwWaitHint                : cardinal;
  33.   end;
  34.   HANDLE = cardinal;
  35.  
  36. const
  37.   SERVICE_QUERY_CONFIG        = $1;
  38.   SERVICE_CHANGE_CONFIG       = $2;
  39.   SERVICE_QUERY_STATUS        = $4;
  40.   SERVICE_START               = $10;
  41.   SERVICE_STOP                = $20;
  42.   SERVICE_ALL_ACCESS          = $f01ff;
  43.   SC_MANAGER_ALL_ACCESS       = $f003f;
  44.   SERVICE_KERNEL_DRIVER       = $1;
  45.   SERVICE_WIN32_OWN_PROCESS   = $10;
  46.   SERVICE_WIN32_SHARE_PROCESS = $20;
  47.   SERVICE_WIN32               = $30;
  48.   SERVICE_INTERACTIVE_PROCESS = $100;
  49.   SERVICE_BOOT_START          = $0;
  50.   SERVICE_SYSTEM_START        = $1;
  51.   SERVICE_AUTO_START          = $2;
  52.   SERVICE_DEMAND_START        = $3;
  53.   SERVICE_DISABLED            = $4;
  54.   SERVICE_DELETE              = $10000;
  55.   SERVICE_CONTROL_STOP        = $1;
  56.   SERVICE_CONTROL_PAUSE       = $2;
  57.   SERVICE_CONTROL_CONTINUE    = $3;
  58.   SERVICE_CONTROL_INTERROGATE = $4;
  59.   SERVICE_STOPPED             = $1;
  60.   SERVICE_START_PENDING       = $2;
  61.   SERVICE_STOP_PENDING        = $3;
  62.   SERVICE_RUNNING             = $4;
  63.   SERVICE_CONTINUE_PENDING    = $5;
  64.   SERVICE_PAUSE_PENDING       = $6;
  65.   SERVICE_PAUSED              = $7;
  66.  
  67. // #######################################################################################
  68. // nt based service utilities
  69. // #######################################################################################
  70. function OpenSCManager(lpMachineName, lpDatabaseName: AnsiString; dwDesiredAccess: cardinal): HANDLE;
  71. external 'OpenSCManagerA@advapi32.dll stdcall';
  72.  
  73. function OpenService(hSCManager: HANDLE; lpServiceName: AnsiString; dwDesiredAccess: cardinal): HANDLE;
  74. external 'OpenServiceA@advapi32.dll stdcall';
  75.  
  76. function CloseServiceHandle(hSCObject: HANDLE): Boolean;
  77. external 'CloseServiceHandle@advapi32.dll stdcall';
  78.  
  79. function CreateService(hSCManager: HANDLE; lpServiceName, lpDisplayName: AnsiString; dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl: cardinal; lpBinaryPathName, lpLoadOrderGroup: AnsiString; lpdwTagId: cardinal; lpDependencies, lpServiceStartName, lpPassword: AnsiString): cardinal;
  80. external 'CreateServiceA@advapi32.dll stdcall';
  81.  
  82. function DeleteService(hService: HANDLE): Boolean;
  83. external 'DeleteService@advapi32.dll stdcall';
  84.  
  85. function StartNTService(hService: HANDLE; dwNumServiceArgs: cardinal; lpServiceArgVectors: cardinal): Boolean;
  86. external 'StartServiceA@advapi32.dll stdcall';
  87.  
  88. function ControlService(hService: HANDLE; dwControl: cardinal; var ServiceStatus: SERVICE_STATUS): Boolean;
  89. external 'ControlService@advapi32.dll stdcall';
  90.  
  91. function QueryServiceStatus(hService: HANDLE; var ServiceStatus: SERVICE_STATUS): Boolean;
  92. external 'QueryServiceStatus@advapi32.dll stdcall';
  93.  
  94. function QueryServiceStatusEx(hService: HANDLE; ServiceStatus: SERVICE_STATUS): Boolean;
  95. external 'QueryServiceStatus@advapi32.dll stdcall';
  96.  
  97.  
  98. function OpenServiceManager(): HANDLE;
  99. begin
  100.   Result := OpenSCManager('', 'ServicesActive', SC_MANAGER_ALL_ACCESS);
  101.   if Result = 0 then
  102.     SuppressibleMsgBox(CustomMessage('msg_ServiceManager'), mbError, MB_OK, MB_OK);
  103. end;
  104.  
  105.  
  106. function InstallService(FileName, ServiceName, DisplayName, Description: AnsiString; ServiceType, StartType: cardinal): Boolean;
  107. var
  108.   hSCM     : HANDLE;
  109.   hService : HANDLE;
  110. begin
  111.   hSCM   := OpenServiceManager();
  112.   Result := False;
  113.   if hSCM <> 0 then begin
  114.     hService := CreateService(hSCM, ServiceName, DisplayName, SERVICE_ALL_ACCESS, ServiceType, StartType, 0, FileName, '', 0, '', '', '');
  115.     if hService <> 0 then begin
  116.       Result := True;
  117.       // Win2K & WinXP supports aditional description text for services
  118.       if Description <> '' then
  119.         RegWriteStringValue(HKLM, 'System\CurrentControlSet\Services\' + ServiceName, 'Description', Description);
  120.       CloseServiceHandle(hService);
  121.     end;
  122.     CloseServiceHandle(hSCM);
  123.   end;
  124. end;
  125.  
  126.  
  127. function RemoveService(ServiceName: String): Boolean;
  128. var
  129.   hSCM     : HANDLE;
  130.   hService : HANDLE;
  131. begin
  132.   hSCM   := OpenServiceManager();
  133.   Result := False;
  134.   if hSCM <> 0 then begin
  135.     hService := OpenService(hSCM, ServiceName, SERVICE_DELETE);
  136.     if hService <> 0 then begin
  137.       Result := DeleteService(hService);
  138.       CloseServiceHandle(hService);
  139.     end;
  140.     CloseServiceHandle(hSCM);
  141.   end;
  142. end;
  143.  
  144.  
  145. function StartService(ServiceName: String): Boolean;
  146. var
  147.   hSCM     : HANDLE;
  148.   hService : HANDLE;
  149. begin
  150.   hSCM   := OpenServiceManager();
  151.   Result := False;
  152.   if hSCM <> 0 then begin
  153.     hService := OpenService(hSCM, ServiceName, SERVICE_START);
  154.     if hService <> 0 then begin
  155.       Result := StartNTService(hService, 0, 0);
  156.       CloseServiceHandle(hService);
  157.     end;
  158.     CloseServiceHandle(hSCM);
  159.   end;
  160. end;
  161.  
  162.  
  163. function StopService(ServiceName: String): Boolean;
  164. var
  165.   hSCM     : HANDLE;
  166.   hService : HANDLE;
  167.   Status   : SERVICE_STATUS;
  168. begin
  169.   hSCM   := OpenServiceManager();
  170.   Result := False;
  171.   if hSCM <> 0 then begin
  172.     hService := OpenService(hSCM, ServiceName, SERVICE_STOP);
  173.     if hService <> 0 then begin
  174.       Result := ControlService(hService, SERVICE_CONTROL_STOP, Status);
  175.       CloseServiceHandle(hService);
  176.     end;
  177.     CloseServiceHandle(hSCM);
  178.   end;
  179. end;
  180.  
  181.  
  182. function IsServiceRunning(ServiceName: String): Boolean;
  183. var
  184.   hSCM     : HANDLE;
  185.   hService : HANDLE;
  186.   Status   : SERVICE_STATUS;
  187. begin
  188.   hSCM   := OpenServiceManager();
  189.   Result := False;
  190.   if hSCM <> 0 then begin
  191.     hService := OpenService(hSCM, ServiceName, SERVICE_QUERY_STATUS);
  192.     if hService <> 0 then begin
  193.       if QueryServiceStatus(hService, Status) then begin
  194.         Result := (Status.dwCurrentState = SERVICE_RUNNING);
  195.       end;
  196.       CloseServiceHandle(hService);
  197.     end;
  198.     CloseServiceHandle(hSCM);
  199.   end;
  200. end;
  201.  
  202.  
  203. function IsServiceStopPending(ServiceName: String): Boolean;
  204. var
  205.   hSCM     : HANDLE;
  206.   hService : HANDLE;
  207.   Status   : SERVICE_STATUS;
  208. begin
  209.   hSCM   := OpenServiceManager();
  210.   Result := False;
  211.   if hSCM <> 0 then begin
  212.     hService := OpenService(hSCM, ServiceName, SERVICE_QUERY_STATUS);
  213.     if hService <> 0 then begin
  214.       if QueryServiceStatus(hService, Status) then begin
  215.         Result := (Status.dwCurrentState = SERVICE_STOP_PENDING);
  216.       end;
  217.       CloseServiceHandle(hService);
  218.     end;
  219.     CloseServiceHandle(hSCM);
  220.   end;
  221. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top