Check if There is a Service

Asked

Viewed 1,212 times

0

Goes below:

function TfPrincipal.ServicoExiste(maquina, servico : PChar) : Boolean;
    var
      SCManHandle, Svchandle : SC_HANDLE;
      {Nome do computador onde esta localizado o serviço}
      sComputerNameEx : string;
      chrComputerName : array[0..255] of char;
      cSize           : Cardinal;
    begin
      {Verifica se nome do computador foi declarado}
      if (maquina = '') then
      begin
        {Caso não tenha sido declarado captura o nome do computador local}
        FillChar(chrComputerName, SizeOf(chrComputerName), #0);
        GetComputerName(chrComputerName, cSize);
        sComputerNameEx:=chrComputerName;
      end
      else
        sComputerNameEx := maquina;

      //ShowMessage(sComputerNameEx);

      SCManHandle := OpenSCManager(PChar(sComputerNameEx), nil, SC_MANAGER_CONNECT);

      if (SCManHandle > 0) then
      begin
        Svchandle := OpenService(SCManHandle, PChar(trim(servico)), SERVICE_QUERY_STATUS);

        //ShowMessage(Svchandle.ToString);
        if (Svchandle <> 0) then
        begin
          result := true;
          //ShowMessage('tem');
          CloseServiceHandle(Svchandle);
        end
        else
        begin
          result := false;
          //ShowMessage('n tem');
          CloseServiceHandle(SCManHandle);
        end;
      end;
    end;

Either it doesn’t work, or I can’t make it work, the problem is it doesn’t come back true even if the service name is correct and installed.

  • gives a Break nos 2 result and rotates the application and where it stops 1º. Apparently this normal!

  • the problem is that in the second Handle it always returns 0;

  • Svchandle := Openservice(Scmanhandle, Pchar(Trim(servico)), SERVICE_QUERY_STATUS); ?

  • exactly. that’s what returns 0.

  • I’ll post a response to how I’m using and running!

  • òtimo!!! thank you very much!

Show 1 more comment

1 answer

2


Well, I’m using it in separate parts, I don’t like to pile it all up, I found this function on the net out there long ago. I believe that its result being always zero is in the way that it is treating the parameters.

function ServiceGetStatus(sMachine, sService: PChar): DWORD;
var
  SCManHandle, SvcHandle: SC_Handle;
  SS: TServiceStatus;
  dwStat: DWORD;
begin
  dwStat := 0;
  // Open service manager handle.
  SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
  if (SCManHandle > 0) then
  begin
    SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
    // if Service installed
    if (SvcHandle > 0) then
    begin
      // SS structure holds the service status (TServiceStatus);
      if (QueryServiceStatus(SvcHandle, SS)) then
        dwStat := ss.dwCurrentState;
      CloseServiceHandle(SvcHandle);
    end;
    CloseServiceHandle(SCManHandle);
  end;
  Result := dwStat;
end;

function ServiceRunning(sMachine, sService: PChar): Boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService);
end;

//Aqui eu verifico se o serviço esta rodando.
procedure TForm1.Button1Click(Sender: TObject);
begin
  if ServiceRunning(nil, 'Servidor') then
    ShowMessage('Servidor esta Online!')
  else
    ShowMessage('Servidor não esta Online!')
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.