tags:

views:

69

answers:

2

Sir..I am trying to check whether window service is working properly or not...i wrote code for that ..its working fine for local system ,but when i used this for remote system..its not working...plz help...code for that are as follows..

//Main Unit...

 ServiceWatcher := TService.Create();
  ShowMessage('Hello ServiceWatcher');

//This is for local system(Which is working fine)

 if( ServiceWatcher.ServiceRunning('','CanveraPushOrder' ) )then
  begin
    ShowMessage('Sevice is running');
  end
  else begin
     ShowMessage('Sevice is not working properly');
  end;
 if(ServiceWatcher.ServiceStopped('','CanveraPushOrder' ) )then
  begin
     ShowMessage('Sevice is stoped');
  end;

//This is for remote system but its not working

  if( ServiceWatcher.ServiceRunning('\\10.30.0.10','OCS inventory service' ) )then
  begin
    ShowMessage('Sevice is running');
  end
  else begin
     ShowMessage('Sevice is not working properly');
  end;

  if(ServiceWatcher.ServiceStopped('\\10.30.0.10','OCS inventory service' ) )then
  begin
     ShowMessage('Sevice is stoped');
  end;

//code for TService

unit ServiceStatus;

interface
uses WinSvc,Windows;


type
  // The customer class definition
  TService = class
    private
    public
     constructor Create;
     function ServiceGetStatus(sMachine,sService : string ) : DWord;
     function ServiceRunning(sMachine, sService : string ) : boolean;
     function ServiceStopped(sMachine, sService : string ) : boolean;
  end;

implementation

constructor TService.Create;
begin

end;
function TService.ServiceGetStatus(sMachine,sService : string ) : DWord;
var
   schm,schs : SC_Handle;
   ss     : TServiceStatus;
   dwStat : DWord;
begin
  //dwStat := -1;
    schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
    if(schm > 0)then
  begin
      schs := OpenService( schm, PChar(sService), SERVICE_QUERY_STATUS);
      if(schs > 0)then
    begin
     if(QueryServiceStatus(schs,ss))then
      begin
        dwStat := ss.dwCurrentState;
     end;
    CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := dwStat;
end;

function TService.ServiceRunning(sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService );
end;

function TService.ServiceStopped(sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_STOPPED =  ServiceGetStatus(sMachine, sService );
end;

end.

please check and tell me where i did wrong....y its not working for remort system?

+1  A: 

If you are author of the services you should add some logs to see what happened. ShowMessage() is not good while it use GUI. Use some kind of logging to text file.

As for checking if service is running you can use sc command. There is example of checking, starting and once again checking of Apache service on remote machine:

c:\tmp>sc \\169.0.1.234 query apache2.2

SERVICE_NAME: apache2.2
    TYPE               : 10  WIN32_OWN_PROCESS
    STATE              : 1  STOPPED
                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)

c:\tmp>sc \\169.0.1.234 start apache2.2

SERVICE_NAME: apache2.2
    TYPE               : 10  WIN32_OWN_PROCESS
    STATE              : 2  START_PENDING
                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
    ...
    PID                : 2844

c:\tmp>sc \\169.0.1.234 query apache2.2

SERVICE_NAME: apache2.2
    TYPE               : 10  WIN32_OWN_PROCESS
    STATE              : 4  RUNNING

Try if checking those services with sc command works.

EDIT:

If sc report some errors then check if service is installed and running on 10.0.30.0 machine. You can check it with services.msc command (GUI services manager) or with net command. Example of net usage on my machine with "Java Quick Starter" running:

C:\tmp>net start | grep Java
   Java Quick Starter

If it is working, then you must check details of sc error message with Bing, Google or other search engine. I think that if you are not be able to check if service is running via sc then your program will fail too.

Michał Niklas
its not working with SC..
Prakash Ranjan
i wrote...c:\tmp>sc \\10.30.0.10 query Java Quick Starter
Prakash Ranjan
but its not workingg...wht should i do..plz tell me
Prakash Ranjan
Michał Niklas
@Prakash: "Java Quick Starter" is only needed on Windows XP and 2000 (http://www.java.com/en/download/help/quickstarter.xml) are you sure it is installed on your remote system? Try all your things using "sc", and see if that works. If it doesn't work with "sc", then it is not a Delphi problem, and you should ask this on http://SuperUser.com or http://ServerFault.com
Jeroen Pluimers
Check if this service is running on 10.30.0.10 as I described in updated answer.
Michał Niklas
A: 

The service manager will only tell you if the service is started and responds to the SCM. It won't tell you anything else - it may not work at all, yet being started. To access the SCM on a remote system you need the proper privileges.

ldsandon