How to check whether a process is running using VB script?

Deadly 提交于 2021-01-28 08:55:04

问题


I have the following VB Script which opens a particular window a performs Certain functions in the window.

WScript.Sleep 10000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Notepad"
#perform some function

WScript.Sleep 10000

I need to repeat same code when notepad is closed so that same notepad is opened again.I tried the following code for that purpose

WScript.Sleep 10000
Set WshShell = WScript.CreateObject("WScript.Shell")
If (WshShell.AppActivate("Notepad") = False) then
ret = True
End If
Do while ret 
WshShell.Run "notepad"
WScript.Sleep 100
WshShell.AppActivate "Notepad"

loop

But this code keeps on opening the notepad even if the previous notepad isn't closed.


回答1:


You can try like this way :

Option Explicit
Dim ProcessPath
ProcessPath = "%Windir%\System32\Notepad.exe"
Call CheckProcess(DblQuote(ProcessPath))
'**************************************************************************
Sub CheckProcess(ProcessPath)
    Dim strComputer,objWMIService,colProcesses,WshShell,Tab,ProcessName
    strComputer = "."
    Tab = Split(ProcessPath,"\")
    ProcessName = Tab(UBound(Tab))
    ProcessName = Replace(ProcessName,Chr(34),"")
    'Msgbox ProcessName
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '"& ProcessName & "'")
    If colProcesses.Count = 0 Then
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run ProcessPath
    Else
        Exit Sub
    End if
End Sub
'**************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

Or like this way :

Option Explicit
Dim ProcessPath,WshShell
ProcessPath = "%Windir%\System32\Notepad.exe"
Set WshShell = CreateObject("WScript.Shell")
If CheckProcess(DblQuote(ProcessPath)) = False Then
    WshShell.Run ProcessPath
End If  
'**************************************************************************
Function CheckProcess(ProcessPath)
    Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
    strComputer = "."
    Tab = Split(ProcessPath,"\")
    ProcessName = Tab(UBound(Tab))
    ProcessName = Replace(ProcessName,Chr(34),"")
    'Msgbox ProcessName
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '"& ProcessName & "'")
    If colProcesses.Count = 0 Then
        CheckProcess = False
    Else
        CheckProcess = True
    End if
End Function
'**************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************


来源:https://stackoverflow.com/questions/32963635/how-to-check-whether-a-process-is-running-using-vb-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!