Follow 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;
Or it does not work, or I'm not able to make it work, the problem is that it does not return true
even though the service name is correct and installed.