当前位置: 主页 > 日志 > VB >

VB 通过WMI实现对系统服务的控制

'项目中用到的代码,分享出来 by redice
'判断服务是否安装
Public Function isInstalled(service As String) As Boolean
     On Error Resume Next
    
     Dim objWMIService   As Object
     Dim colProcessList
     Dim objProcess      As Object
    
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
     Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
    
     If colProcessList.Count > 0 Then
        isInstalled = True
     Else
        isInstalled = False
     End If
   
     Set objWMIService = Nothing
     Set objProcess = Nothing
End Function

'判断服务是否停止
Public Function isStopped(service As String) As Boolean
  On Error Resume Next
  isStopped = False
 
  Dim objWMIService   As Object
  Dim colProcessList
  Dim objProcess      As Object
 
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
  Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
 
  For Each objProcess In colProcessList
     If LCase(objProcess.State) = "stopped" Then
        isStopped = True
        Set objWMIService = Nothing
        Set objProcess = Nothing
        Exit Function
     End If
  Next

  Set objWMIService = Nothing
  Set objProcess = Nothing
End Function


'停止服务
Public Sub stopService(service As String)
  On Error Resume Next
  Dim objWMIService   As Object
  Dim colProcessList
  Dim objProcess      As Object
 
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
  Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
 
  For Each objProcess In colProcessList
      objProcess.stopService
  Next

  Set objWMIService = Nothing
  Set objProcess = Nothing
End Sub

启动服务
Public Sub startService(service As String)
  On Error Resume Next
  Dim objWMIService   As Object
  Dim colProcessList
  Dim objProcess      As Object
 
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
  Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
 
  For Each objProcess In colProcessList
      objProcess.startService
  Next

  Set objWMIService = Nothing
  Set objProcess = Nothing
End Sub

'禁用服务
Public Sub disableService(service As String)
  On Error Resume Next
  Dim objWMIService   As Object
  Dim colProcessList
  Dim objProcess      As Object
 
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
  Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
 
  For Each objProcess In colProcessList
      objProcess.ChangeStartMode ("Disabled")
  Next

  Set objWMIService = Nothing
  Set objProcess = Nothing
End Sub

'启用服务
Public Sub enableService(service As String)
  On Error Resume Next
  Dim objWMIService   As Object
  Dim colProcessList
  Dim objProcess      As Object
 
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
  Set colProcessList = objWMIService.ExecQuery("select * from  win32_service  where  name='" & service & "'")
 
  For Each objProcess In colProcessList
      objProcess.ChangeStartMode ("Automatic")
  Next

  Set objWMIService = Nothing
  Set objProcess = Nothing
End Sub

[日志信息]

该日志于 2010-03-01 14:09 由 redice 发表在 redice's Blog ,你除了可以发表评论外,还可以转载 “VB 通过WMI实现对系统服务的控制” 日志到你的网站或博客,但是请保留源地址及作者信息,谢谢!!    (尊重他人劳动,你我共同努力)
   
验证(必填):   点击我更换验证码

redice's Blog  is powered by DedeCms |  Theme by Monkeii.Lee |  网站地图 |  本服务器由西安鲲之鹏网络信息技术有限公司友情提供

返回顶部