Reply to comment

Windows Service Status

Relevant MSDN links:

This is sample code. Add error handling and adjust to your requirements as necessary.

* 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 
* 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	

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.