//方法1
uses WinSVC;
function isServiceRunning(ServiceName : AnsiString): Boolean;
var
aServiceControl : SC_Handle;
aService : SC_Handle;
status: TServiceStatus;
begin
Result := False;
aService := 0;
aServiceControl := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if aServiceControl = 0 then Exit;
try
aService := OpenService(aServiceControl, PAnsiChar(ServiceName), SERVICE_QUERY_STATUS);
if aService = 0 then Exit;
if not QueryServiceStatus(aService, status)
then Exit;
Result := status.dwCurrentState = SERVICE_RUNNING;
finally
if aService <> 0 then CloseServiceHandle(aService);
if aServiceControl <> 0 then CloseServiceHandle(aServiceControl)
end;
end;
//方法2
uses ActiveX,ComObj;
function isServiceRunning(ServiceName: string): Boolean;
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT Name, State FROM Win32_Service WHERE Name="'+ServiceName+'" and State="Running"','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = S_OK then
begin
Result := True;
FWbemObject := Unassigned;
end
else
Result := False;
end;