本文整理自网络,侵删。
//方法1uses 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;
//方法2uses 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;
相关阅读 >>
Delphi android实例-读取设备联系人(xe8+小米2)
Delphi 2009 之 tcategorypanelgroup[1]: chevronalignment 等
更多相关阅读请进入《Delphi》频道 >>