Advertisement
TLama

Untitled

Sep 15th, 2015
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.77 KB | None | 0 0
  1. const
  2.   ...
  3.   SERVICE_QUERY_CONFIG = $0001;
  4.  
  5.   SERVICE_BOOT_START = $00000000;
  6.   SERVICE_SYSTEM_START = $00000001;
  7.   SERVICE_AUTO_START = $00000002;
  8.   SERVICE_DEMAND_START = $00000003;
  9.   SERVICE_DISABLED = $00000004;
  10.  
  11.   ERROR_INSUFFICIENT_BUFFER = 122;
  12.  
  13. function QueryServiceConfig(hService: TSCHandle; lpServiceConfig: array of Byte;
  14.   cbBufSize: DWORD; out pcbBytesNeeded: DWORD): BOOL;
  15.   external 'QueryServiceConfig{#AW}@advapi32.dll stdcall';
  16.  
  17. function GetDwordValue(const Source: array of Byte; Offset: Integer): DWORD;
  18. begin
  19.   Result := DWORD(Source[Offset]) + DWORD(Source[Offset + 1]) shl 8 +
  20.     DWORD(Source[Offset + 2]) shl 16 + DWORD(Source[Offset + 3]) shl 24;
  21. end;
  22.  
  23. function GetServiceStartType(const SvcName: string): DWORD;
  24. var
  25.   Buffer: array of Byte;
  26.   BufSize: DWORD;
  27.   Manager: TSCHandle;
  28.   Service: TSCHandle;
  29. begin
  30.   Manager := OpenSCManager('', '', SC_MANAGER_CONNECT);
  31.   if Manager <> 0 then
  32.   try
  33.     Service := OpenService(Manager, SvcName, SERVICE_QUERY_CONFIG);
  34.     if Service <> 0 then
  35.     try
  36.       SetLength(Buffer, 0);
  37.       if not Boolean(QueryServiceConfig(Service, Buffer, 0, BufSize)) and (DLLGetLastError = ERROR_INSUFFICIENT_BUFFER) then
  38.       begin
  39.         SetLength(Buffer, BufSize);
  40.         if QueryServiceConfig(Service, Buffer, BufSize, BufSize) then
  41.           // dwStartType member is after a DWORD long member dwServiceType, hence the 4B shift
  42.           Result := GetDwordValue(Buffer, 4)
  43.         else
  44.           RaiseException('QueryServiceStatus failed. ' + SysErrorMessage(DLLGetLastError));
  45.       end
  46.       else
  47.         RaiseException('QueryServiceStatus failed. ' + SysErrorMessage(DLLGetLastError));
  48.     finally
  49.       CloseServiceHandle(Service);
  50.     end
  51.     else
  52.       RaiseException('OpenService failed. ' + SysErrorMessage(DLLGetLastError));
  53.   finally
  54.     CloseServiceHandle(Manager);
  55.   end
  56.   else
  57.     RaiseException('OpenSCManager failed. ' + SysErrorMessage(DLLGetLastError));
  58. end;
  59.  
  60. // and an example call
  61. try
  62.   case GetServiceStartType('RemoteAccess') of
  63.     SERVICE_BOOT_START: MsgBox('A device driver started by the system loader.', mbInformation, MB_OK);
  64.     SERVICE_SYSTEM_START: MsgBox('A device driver started by the IoInitSystem function.', mbInformation, MB_OK);
  65.     SERVICE_AUTO_START: MsgBox('A service started automatically by the service control manager during system startup.', mbInformation, MB_OK);
  66.     SERVICE_DEMAND_START: MsgBox('A service started by the service control manager when a process calls the StartService function.', mbInformation, MB_OK);
  67.     SERVICE_DISABLED: MsgBox('A service that cannot be started.', mbInformation, MB_OK);
  68.   else
  69.     RaiseException('GetServiceStartType returned unknown start type.');
  70.   end;
  71. except
  72.   MsgBox(GetExceptionMessage, mbError, MB_OK);
  73. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement