Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const
- ...
- SERVICE_QUERY_CONFIG = $0001;
- SERVICE_BOOT_START = $00000000;
- SERVICE_SYSTEM_START = $00000001;
- SERVICE_AUTO_START = $00000002;
- SERVICE_DEMAND_START = $00000003;
- SERVICE_DISABLED = $00000004;
- ERROR_INSUFFICIENT_BUFFER = 122;
- function QueryServiceConfig(hService: TSCHandle; lpServiceConfig: array of Byte;
- cbBufSize: DWORD; out pcbBytesNeeded: DWORD): BOOL;
- external 'QueryServiceConfig{#AW}@advapi32.dll stdcall';
- function GetDwordValue(const Source: array of Byte; Offset: Integer): DWORD;
- begin
- Result := DWORD(Source[Offset]) + DWORD(Source[Offset + 1]) shl 8 +
- DWORD(Source[Offset + 2]) shl 16 + DWORD(Source[Offset + 3]) shl 24;
- end;
- function GetServiceStartType(const SvcName: string): DWORD;
- var
- Buffer: array of Byte;
- BufSize: DWORD;
- Manager: TSCHandle;
- Service: TSCHandle;
- begin
- Manager := OpenSCManager('', '', SC_MANAGER_CONNECT);
- if Manager <> 0 then
- try
- Service := OpenService(Manager, SvcName, SERVICE_QUERY_CONFIG);
- if Service <> 0 then
- try
- SetLength(Buffer, 0);
- if not Boolean(QueryServiceConfig(Service, Buffer, 0, BufSize)) and (DLLGetLastError = ERROR_INSUFFICIENT_BUFFER) then
- begin
- SetLength(Buffer, BufSize);
- if QueryServiceConfig(Service, Buffer, BufSize, BufSize) then
- // dwStartType member is after a DWORD long member dwServiceType, hence the 4B shift
- Result := GetDwordValue(Buffer, 4)
- else
- RaiseException('QueryServiceStatus failed. ' + SysErrorMessage(DLLGetLastError));
- end
- else
- RaiseException('QueryServiceStatus failed. ' + SysErrorMessage(DLLGetLastError));
- finally
- CloseServiceHandle(Service);
- end
- else
- RaiseException('OpenService failed. ' + SysErrorMessage(DLLGetLastError));
- finally
- CloseServiceHandle(Manager);
- end
- else
- RaiseException('OpenSCManager failed. ' + SysErrorMessage(DLLGetLastError));
- end;
- // and an example call
- try
- case GetServiceStartType('RemoteAccess') of
- SERVICE_BOOT_START: MsgBox('A device driver started by the system loader.', mbInformation, MB_OK);
- SERVICE_SYSTEM_START: MsgBox('A device driver started by the IoInitSystem function.', mbInformation, MB_OK);
- SERVICE_AUTO_START: MsgBox('A service started automatically by the service control manager during system startup.', mbInformation, MB_OK);
- SERVICE_DEMAND_START: MsgBox('A service started by the service control manager when a process calls the StartService function.', mbInformation, MB_OK);
- SERVICE_DISABLED: MsgBox('A service that cannot be started.', mbInformation, MB_OK);
- else
- RaiseException('GetServiceStartType returned unknown start type.');
- end;
- except
- MsgBox(GetExceptionMessage, mbError, MB_OK);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement