Windows Service Status

Relevant MSDN links:

$SAMPLECODE$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

* Using Shell Application object
* Service name to check
lcServiceName = "MSSQLSERVER" 
oShellApp = CreateObject("Shell.Application")
IF NOT oShellApp.IsServiceRunning(lcServiceName)
	? "Service isn't running" 
	*  Try to start 
	IF oShellApp.ServiceStart(lcServiceName,.F.)
		? "Service startrted" 
	ELSE
		? "Couldn't start service" 
	ENDIF
ELSE
	? "Service is running" 
	*  Try to stop 
	IF oShellApp.ServiceStop(lcServiceName, .F.)
		? "Service stoped" 
	ELSE
		? "Couldn't stop service" 
	ENDIF
ENDIF 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

* Using WIN API
* Service name to check
lcServiceName = "MSSQLSERVER"

* Service Control Manager object specific access types
#define SC_MANAGER_CONNECT             0x0001
#define SC_MANAGER_CREATE_SERVICE      0x0002
#define SC_MANAGER_ENUMERATE_SERVICE   0x0004
#define SC_MANAGER_LOCK                0x0008
#define SC_MANAGER_QUERY_LOCK_STATUS   0x0010
#define SC_MANAGER_MODIFY_BOOT_CONFIG  0x0020

* Service object specific access type
#define SERVICE_QUERY_CONFIG           0x0001
#define SERVICE_CHANGE_CONFIG          0x0002
#define SERVICE_QUERY_STATUS           0x0004
#define SERVICE_ENUMERATE_DEPENDENTS   0x0008
#define SERVICE_START                  0x0010
#define SERVICE_STOP                   0x0020
#define SERVICE_PAUSE_CONTINUE         0x0040
#define SERVICE_INTERROGATE            0x0080
#define SERVICE_USER_DEFINED_CONTROL   0x0100

* Service State 
#define SERVICE_STOPPED                0x00000001
#define SERVICE_START_PENDING          0x00000002
#define SERVICE_STOP_PENDING           0x00000003
#define SERVICE_RUNNING                0x00000004
#define SERVICE_CONTINUE_PENDING       0x00000005
#define SERVICE_PAUSE_PENDING          0x00000006
#define SERVICE_PAUSED                 0x00000007

DECLARE Long OpenSCManager IN Advapi32 ;
	  STRING lpMachineName, STRING lpDatabaseName, Long dwDesiredAccess
DECLARE Long OpenService IN Advapi32 ;
	  Long hSCManager, String lpServiceName, Long dwDesiredAccess
DECLARE Long QueryServiceStatus IN Advapi32 ;
	  Long hService, String @ lpServiceStatus
DECLARE Long CloseServiceHandle  IN Advapi32 ;
	  Long hSCObject

lhSCManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT + SC_MANAGER_ENUMERATE_SERVICE)
IF lhSCManager = 0
	* Error
ENDIF

lhSChandle = OpenService(lhSCManager, lcServiceName, SERVICE_QUERY_STATUS)
IF lhSCManager = 0
	* Error
ENDIF
lcQueryBuffer = REPLICATE(CHR(0), 4*7 )
lnRetVal = QueryServiceStatus(lhSChandle, @lcQueryBuffer )
IF lnRetVal = 0
	* Error
ENDIF
* Close Handles
CloseServiceHandle(lhSChandle)  
CloseServiceHandle(lhSCManager)  
lnServiceStatus = ASC(SUBSTR(lcQueryBuffer,5,1))
IF lnServiceStatus <> SERVICE_RUNNING
	* Service isn't running
	? "Service isn't running"
ENDIF	


Comments

The Windows Services can be enumerated using Windows API <a href="http://msdn.microsoft.com/en-us/library/ms682637(VS.85).aspx">EnumServicesStatus function</a> or WMI <a href="http://msdn.microsoft.com/en-us/library/aa394418(VS.85).aspx">Win32_Service class</a>. The later is used in the code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

loLocator 	= CREATEOBJECT('WBEMScripting.SWBEMLocator')
loWMI		= loLocator.ConnectServer() 
loWMI.Security_.ImpersonationLevel = 3  		&amp;&amp; Impersonate
 
loWinServices =  loWMI.ExecQuery("Select * from Win32_Service ")

FOR EACH loWinService IN loWinServices 
	WITH loWinService
		? .Name, .Displayname, .StartMode, .State, .Status
	ENDWITH	
ENDFOR