我需要知道进程 ID 是否存在。到目前为止的代码:
Option Explicit
Dim WshShell As Object
Dim EngineRun As Object
Sub main()
Set WshShell = CreateObject("WScript.Shell")
Set EngineRun = WshShell.Exec("notepad.exe")
MsgBox EngineRun.ProcessID
If EngineRun.ProcessID = True Then
WshShell.run "TASKKILL /F /IM " & EngineRun.ProcessID, , True
MsgBox EngineRun.ProcessID & (" terminated")
Else
MsgBox EngineRun.ProcessID & (" does not exist")
End If
End Sub
如果我取出“If”语句并让它打开记事本并显示一个带有进程 ID 的 MsgBox,那么当我单击 MsgBox 的“确定”时,它将成功杀死记事本
但是使用“If”语句,无论记事本是否打开,它都会显示“不存在”MsgBox。如果它是打开的,它会成功杀死它,然后显示“不存在”MsgBox,而不是“已终止”MsgBox。
任何想法都非常感谢!
最佳答案
试试这个
Option Explicit
Dim WshShell As Object
Dim EngineRun As Object
Dim objWMIService As Object
Dim colProcessList As Object
Sub Main
Set WshShell = CreateObject("WScript.Shell")
Set EngineRun = WshShell.Exec("notepad.exe")
MsgBox EngineRun.ProcessID
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessID = " & EngineRun.ProcessID)
If colProcessList.Count = 1 Then
WshShell.run "TASKKILL /F /IM " & EngineRun.ProcessID, , True
MsgBox EngineRun.ProcessID & (" terminated")
Else
MsgBox EngineRun.ProcessID & (" does not exist")
End If
End Sub
关于process - VB6如何检查进程ID是否已终止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39606376/