• 欢迎访问金刀客博客!
  • 2019,春节快乐!

vb中利用WMI判断系统服务状态

原创天空 admin 4561次浏览 已收录 0个评论


首先引用microsoft wmi scripting v1.2 library

Private Sub Command1_Click()
On Error GoTo err
Dim objSWbemLocator As SWbemLocator
Dim objSWbemServices As SWbemServices
Dim objSWbemObjectSet As SWbemObjectSet
Dim objSWbemObject As SWbemObject
Dim strComputer As String, strNameSpace As String, strClass As String
Dim sstr As String
sstr = Text2.Text
‘判断服务是否启动
strComputer = “.”
strNameSpace = “root\cimv2”
strClass = “Win32_Service”
Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace)
Set objSWbemObjectSet = objSWbemServices.ExecQuery(“Select * FROM ” & strClass & ” where name='” & sstr & “‘”)
For Each objSWbemObject In objSWbemObjectSet
Text1.Text = “服务状态:” + objSWbemObject.State _
+ Chr(13) + Chr(10) + “启动方式:” + objSWbemObject.StartMode
Next
Exit Sub
err:
Text1.Text = “服务名不对!”
End Sub

如果服务已经启动需要关闭把后面这样修改,如防火墙

For Each objSWbemObject In objSWbemObjectSet
Text1.Text = “系统防火墙状态:” + objSWbemObject.State + ” 启动方式:” + objSWbemObject.StartMode + “..”
fwstatus = objSWbemObject.State
Next
If fwstatus = “Running” Then
Text1.Text = “系统防火墙开启,将被自动关闭……”
Shell “net stop sharedaccess”
End If

点击下载源文件


金刀客博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明vb中利用WMI判断系统服务状态
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到