admin 发表于 2010-12-25 17:12:06

VB 判断指定名字的进程是否存在函数


'=============================================================
'函数名:CheckApplicationIsRun
'作用:判断进程是否存在
'参数:szExeFileName   ----进程名字
'返回值:布尔值   ----True进程存在,False不存在
'=============================================================
Function CheckApplicationIsRun(ByVal szExeFileName As String) As Boolean
      On Error GoTo Err
      Dim WMI
      Dim Obj
      Dim Objs
      CheckApplicationIsRun = False
      Set WMI = GetObject("WinMgmts:")
      Set Objs = WMI.InstancesOf("Win32_Process")
      For Each Obj In Objs
               If InStr(UCase(szExeFileName), UCase(Obj.Description)) <> 0 Then
                     CheckApplicationIsRun = True
                     If Not Objs Is Nothing Then Set Objs = Nothing
                     If Not WMI Is Nothing Then Set WMI = Nothing
                     Exit Function
               End If
      Next
      If Not Objs Is Nothing Then Set Objs = Nothing
      If Not WMI Is Nothing Then Set WMI = Nothing
      Exit Function
   Err:
      If Not Objs Is Nothing Then Set Objs = Nothing
      If Not WMI Is Nothing Then Set WMI = Nothing
End Function
'测试
Private Sub Command1_Click()
      If CheckApplicationIsRun("notepad.exe") = True Then
            MsgBox "已经运行了记事本程序"
      Else
            MsgBox "记事本程序没有运行"
      End If
End Sub
页: [1]
查看完整版本: VB 判断指定名字的进程是否存在函数