Checking window service are working properly
I am trying to check whether window service is working properly or not. I wrote code for that; it's working fine for local system, but when I used this for remote system, it's not working.
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开发者_StackOverflow社区, 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 went wrong?
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.
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.
精彩评论