Windows Service Status
By Sergey - Posted on January 1st, 2008
Relevant MSDN links:
- Windows System Services - Service Functions
- Windows Shell - IsServiceRunning Method
- Windows Shell - ServiceStart Method
- Windows Shell - ServiceStop Method
| 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
Recent comments
1 week 1 day ago
2 weeks 1 day ago
3 weeks 2 days ago
3 weeks 3 days ago
3 weeks 5 days ago
3 weeks 6 days ago
4 weeks 4 days ago
4 weeks 4 days ago
4 weeks 5 days ago
4 weeks 5 days ago