如何啟動停止Windows服務

出自VFP Wiki

在2006年9月29日 (五) 14:10由Elleryq (對話 | 貢獻)所做的修訂版本
(差異) ←上一修訂 | 最新修訂 (差異) | 下一修訂→ (差異)
跳轉到: 導航, 搜尋


如何啟動停止Windows服務

*
* AppDev: Something You Should Know by Irena Kennedy : SYSK 197: Smart Start and Stop Service in VS.NET Build
* http://blogs.msdn.com/irenak/archive/2006/09/13/752075.aspx
*

DO ServiceList
? Service( "start", "W3SVC" )
? ServiceStatus( "W3SVC" )
? Service( "stop", "W3SVC" )
? ServiceStatus( "W3SVC" )

PROCEDURE ServiceList
LOCAL WMIService
LOCAL Services
LOCAL Service

WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Services = WMIService.ExecQuery("Select * from Win32_Service" )

For Each Service In Services
	? Service.Name + " " + Service.State
NEXT
ENDPROC

FUNCTION Service
LPARAMETERS action, serviceName

LOCAL WMIService
LOCAL Services
LOCAL Service
LOCAL result

result = .f.

WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Services = WMIService.ExecQuery("Select * from Win32_Service where Name = '" + serviceName + "'" )

IF Services.Count = 1 THEN 
    For Each Service In Services
    	DO CASE
    		CASE action == "start"
    			IF Service.State <> "Running" THEN 
    				result = Service.StartService()
    			ENDIF
    		CASE action == "stop"
    			IF Service.State <> "Stopped" THEN
    				result = Service.StopService()
    			ENDIF 
    	ENDCASE 
    NEXT
ENDIF

RETURN result 
ENDFUNC 

FUNCTION ServiceStatus
LPARAMETERS serviceName
LOCAL WMIService
LOCAL Services
LOCAL Service
LOCAL result 

result = ""
WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Services = WMIService.ExecQuery("Select * from Win32_Service where Name = '" + serviceName + "'" )

IF Services.Count = 1 THEN 
    For Each Service In Services
		result = Service.State
	NEXT 
ENDIF 

RETURN result 
ENDFUNC