Visual Basic 中判斷指定名稱的進程是否存在

簡潔版:

Dim Ename
For Each Ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ '循環進程
    If LCase(Ename.name) = LCase("CSTRIKE-ONLINE.EXE") Then MsgBox "請先關掉遊戲,否則無法開啓外掛!", vbInformation, "提示:"
Next

---------------------------------------------------------------------------

囉嗦版:

Function CheckApplicationIsRun(ByVal szExeFileName As String) As Boolean
    On Error GoTo Err
    Dim WMI, Obj, 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章