function TForm1.GetServiceStatus(const ServiceName: string): DWORD;
var
schService, schSCManager: DWORD;
ServiceStatus: TServiceStatus;
begin
schSCManager := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if schSCManager = 0 then RaiseLastWin32Error;
try
schService := OpenService(schSCManager, PChar(ServiceName),
SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if schService = 0 then RaiseLastWin32Error;
try
if not QueryServiceStatus(schService, ServiceStatus) then
RaiseLastWin32Error;
Result := ServiceStatus.dwCurrentState;
finally
CloseServiceHandle(schService);
end;
finally
CloseServiceHandle(schSCManager);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
case GetServiceStatus('Messenger') of
SERVICE_STOPPED, SERVICE_STOP_PENDING:
begin
Caption := 'It''s Stoped';
end;
SERVICE_START_PENDING, SERVICE_CONTINUE_PENDING, SERVICE_RUNNING:
begin
Caption := 'It''s Running';
end;
SERVICE_PAUSE_PENDING, SERVICE_PAUSED:
begin
Caption := 'It''s Paused';
end;
end;
end;
function TForm1.GetServiceStatus(const ServiceName: string): DWORD;
var
schService, schSCManager: DWORD;
ServiceStatus: TServiceStatus;
begin
schSCManager := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if schSCManager = 0 then RaiseLastWin32Error;
try
schService := OpenService(schSCManager, PChar(ServiceName),
SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if schService = 0 then RaiseLastWin32Error;
try
if not QueryServiceStatus(schService, ServiceStatus) then
RaiseLastWin32Error;
Result := ServiceStatus.dwCurrentState;
finally
CloseServiceHandle(schService);
end;
finally
CloseServiceHandle(schSCManager);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
case GetServiceStatus('Messenger') of
SERVICE_STOPPED, SERVICE_STOP_PENDING:
begin
Caption := 'It''s Stoped';
end;
SERVICE_START_PENDING, SERVICE_CONTINUE_PENDING, SERVICE_RUNNING:
begin
Caption := 'It''s Running';
end;
SERVICE_PAUSE_PENDING, SERVICE_PAUSED:
begin
Caption := 'It''s Paused';
end;
end;
end;
have a nice day :D