Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with Windows Services using Delphi

Status
Not open for further replies.

minckle

Programmer
Mar 17, 2004
142
GB
I have two computers networked to each other - computer A and computer B.

Computer A has a Windows Service on called 'svcItextIt1'

My aim is to write a program for Computer B, to look at Computer A and see if it has the service installed correctly.

If a put my program on computer A it works fine with the machine name set as :
Machine Name: Blank
I.P Address
sSkills3 (name of computer)

but, no matter what I set machine name to ( I.P address or computer A name ), the service is not being recognised when I run the program on computer B.

Is this possible to do? Could it a network problem?

Please can anyone help or advise me


The code I am using is below


procedure TfrmServiceStatus.btnOKClick(Sender: TObject);
begin
machinename := edit1.text;
servicename := 'svcItextIt1';

if serviceexists(machinename,servicename) then
begin
showmessage('svcItextIt1 DOES exist');
end
else
begin
showmessage('svcItextIt1 DOES NOT exist');
end;
end;

function TfrmServiceStatus.serviceExists(sMachine,sService:string):boolean;
var
schm, schs : SC_Handle;
begin
schm :=0;
schs := 0;

schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);

if (schm <>0) then
schs := OpenService( schm, PChar(sService), SERVICE_ALL_ACCESS)
else
result:= (schs<>0);
end;
 
Hi

My idea is that you don't have enough permissions to open service with SERVICE_ALL_ACCESS flag. Try opening it for GENERIC_READ, that access is enough for checking if the service exists.
Here's slightly modified code of yours that works okay on my side.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  aSC,aSVc : Cardinal;
begin
  aSC := OpenSCManager(pChar(Edit1.Text), nil, SC_MANAGER_CONNECT);
  if (aSC <> 0) then ShowMessage ('Remote SC Opened')
  else ShowMessage ('Failed to open remote SC')
  aSvc := OpenService(aSC, pChar(Edit2.Text), GENERIC_READ);
  if aSVc <> 0 then ShowMessage('SVC opened')
  else ShowMessage('Failed to open SVC');
end;
Best wishes

--- McMerfy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top