function ServiceStart(const ServiceName: string): Boolean;
var SCM, SCH: SC_Handle;
P: PChar;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
Result := StartService(SCH, 0, P);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function ServiceStop(const ServiceName: string): Boolean;
var
SCM, SCH: SC_Handle;
ServiceStatus: TServiceStatus;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
Result := ControlService(SCH, SERVICE_CONTROL_STOP, ServiceStatus);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function ServiceShutdown(const ServiceName: string): Boolean;
var
SCM, SCH: SC_Handle;
ServiceStatus: TServiceStatus;
begin
Result := False;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
Result := ControlService(SCH, SERVICE_CONTROL_SHUTDOWN, ServiceStatus);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function ServiceStatus(const ServiceName: string): integer;
var
SCM, SCH: SC_Handle;
ServiceStatus: TServiceStatus;
begin
Result := 0;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
QueryServiceStatus(SCH, ServiceStatus);
Result := ServiceStatus.dwCurrentState;
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function ServiceRunning(const ServiceName: string): Boolean;
begin
Result := ServiceStatus(ServiceName) = SERVICE_RUNNING;
end;
function ServiceStopped(const ServiceName: string): Boolean;
begin
Result := ServiceStatus(ServiceName) = SERVICE_STOPPED;
end;
function ServiceDisplayName(const ServiceName: string): string;
var
SCM, SCH: SC_Handle;
L: DWORD;
begin
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
GetServiceDisplayName(SCM, PChar(ServiceName), nil, L);
SetLength(Result, L);
GetServiceDisplayName(SCM, PChar(ServiceName), PChar(Result), L);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function ServiceFileName(const Name: string): string;
var
SCM, SCH: SC_Handle;
ServiceConfig: PQueryServiceConfig;
R: DWORD;
begin
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(Name), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
QueryServiceConfig(SCH, nil, 0, R);
GetMem(ServiceConfig, R + 1);
QueryServiceConfig(SCH, ServiceConfig, R + 1, R);
Result := ServiceConfig.lpBinaryPathName;
FreeMem(ServiceConfig);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
function QueryServiceConfig2(hService: SC_HANDLE; dwInfoLevel: DWORD;
lpBuffer: PChar; cbBufSize: DWORD; var pcbBytesNeeded: DWORD): BOOL;
stdcall; external advapi32 name 'QueryServiceConfig2A';
function ServiceDescription(const ServiceName: string): string;
const
SERVICE_CONFIG_DESCRIPTION = $00000001;
type
TServiceDescription = packed record
lpDescription: LPSTR;
end;
PServiceDescription = ^TServiceDescription;
var
SCM, SCH: SC_Handle;
P: PServiceDescription;
R: DWORD;
begin
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
QueryServiceConfig2(SCH, SERVICE_CONFIG_DESCRIPTION, nil, 0, R);
GetMem(P, R + 1);
QueryServiceConfig2(SCH, SERVICE_CONFIG_DESCRIPTION, PChar(P), R + 1, R);
Result := P.lpDescription;
FreeMem(P);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
procedure ServiceNames(Names: TStrings; DisplayNames: TStrings = nil; const uType: integer = SERVICE_WIN32);
type
TEnumServices = array[0..0] of TEnumServiceStatus;
PEnumServices = ^TEnumServices;
var
SCM: SC_Handle;
Services: PEnumServices;
Len: Cardinal;
ServiceCount, ResumeHandle, i: Cardinal;
begin
ResumeHandle := 0;
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
Len := 0;
ServiceCount := 0;
Services := nil;
try
Names.BeginUpdate;
if Assigned(DisplayNames) then DisplayNames.BeginUpdate;
if SCM <> 0 then
begin
EnumServicesStatus(SCM, uType, SERVICE_STATE_ALL,
Services[0], 0, Len, ServiceCount, ResumeHandle);
GetMem(Services, Len);
EnumServicesStatus(SCM, SERVICE_DRIVER or SERVICE_WIN32, SERVICE_STATE_ALL,
Services[0], Len, Len, ServiceCount, ResumeHandle);
for i := 0 to ServiceCount - 1 do
begin
Names.Add(Services[i].lpServiceName);
if Assigned(DisplayNames) then DisplayNames.Add(Services[i].lpDisplayName);
end;
FreeMem(Services);
end;
finally
Names.EndUpdate;
if Assigned(DisplayNames) then DisplayNames.EndUpdate;
CloseServiceHandle(SCM);
end;
end;