[VB6] 利用WMI進行 NT Service Control

  • 21544
  • 0
  • JAVA
  • 2008-11-16

摘要:[VB6] 利用WMI進行 NT Service Control

Windows 管理規範(Windows Management Instrumentation)是一項核心的 Windows 管理技術;用戶可以使用 WMI 管理本機和遠端電腦。利用WMI類別進行 NT Service Control,WMI類別可提供給各個程式語言引用,請善用Google搜尋器。
http://msdn.microsoft.com/en-us/library/aa394418(VS.85).aspx
這是WMI Win32_Service,提供了12個method,25個Property,詳細設定請看官網!

Method:
01.StartService
02.StopService
03.PauseService
04.ResumeService
05.InterrogateService
06.UserControlService
07.Create
08.Change
09.ChangeStartMode
10.Delete
11.GetSecurityDescriptor
12SetSecurityDescriptor

Property:
01.AcceptPause
02.AcceptStop
03.Caption
04.CheckPoint
05.CreationClassName
06.Description
07.DesktopInteract
08.DisplayName
09.ErrorControl
10.ExitCode
11.InstallDate
12.Name
13.PathName
14.ProcessId
15.ServiceSpecificExitCode
16.ServiceType
17.Started
18.StartMode
19.StartName
20.State
21.Status
22.SystemCreationClassName
23.SystemName
24.TagId
25.WaitHint

落落長的說明,見到就整個人無力了,不過別輕言放棄,今天要來實地操演其中幾個方法。

本範例用VB6來進行演練
1.首先建立WMI問句
    Set objWMISrv = GetObject _
        ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objSrvList = objWMISrv.ExecQuery _
        ("Select * from Win32_Service where Name=" & "'" & SrvName & "'" & "")
    strComputer 變數代表電腦名稱,SrvName 變數表示服務名稱



2.再來使用方法
    2-1 啟用服務
    For Each objSrv In objSrvList
        errReturn = objSrv.StartService()
        StartService = StatusOutput("Start Service")
        Debug.Print StatusOutput("Start")
    Next

    2-2 停止服務
    For Each objSrv In objSrvList
        errReturn = objSrv.StopService()
        StopService = StatusOutput("Stop Service")
        Debug.Print StatusOutput("Stop")
    Next
 
    2-3 移除服務
    For Each objSrv In objSrvList
        errReturn = objSrv.StopService()
        Debug.Print StatusOutput("Stop Service")
        errReturn = objSrv.Delete()
        RemoveService = StatusOutput("Remove Service")
        Debug.Print StatusOutput("Remove Service")
    Next
   
    2-4 暫停服務
    For Each objSrv In objSrvList
        errReturn = objSrv.PauseService()
        PauseService = StatusOutput("Pause Service")
        Debug.Print StatusOutput("Pause Service")
    Next

範例下載
Service Control.rar

範例畫面

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo