ThangLe
ThangLe

Reputation: 970

How can I get process if it's already running?

For example:

I have a code of VBS, it will help me check a program is already running

Function isProcessRunning(strComputer, strProcess)
    Dim Process, strObject
    strObject = "winmgmts://" & strComputer

    For Each Process In GetObject(strObject).InstancesOf("Win32_Process")
        If UCase(Process.Name) = UCase(strProcess) Then
            isProcessRunning = True
            Exit Function
        Else
            isProcessRunning = False
        End If
    Next
End Function

But, I want to get process is already running for re-using.

Function shell()
        If isProcessRunning(".", "cmd.exe") == False Then
            Dim objShell
            Set objShell = WScript.CreateObject("WScript.Shell")
            shell = objShell.Run(cmd)
        Else
            shell = ...  'how can I get cmd.exe already running here
        End If
    Next
End Function

Update my problem

Actually, I want to get instance of cmd.exe if it's already running. I mean, I don't want re-open cmd.exe again.

Example:

  1. I have just opened cmd.exe.
  2. I run this code below (its name is laucher.vbs).
Function isProcessRunning(strComputer, strProcess)
    Dim Process, strObject

    strObject = "winmgmts://" & strComputer

    For Each Process In GetObject(strObject).InstancesOf("Win32_Process")
        If UCase(Process.Name) = UCase(strProcess) Then
            isProcessRunning = True
            Exit Function
        Else
            isProcessRunning = False
        End If
    Next
End Function

Function getProcessRunning(strComputer, strProcess)
    Dim Process, strObject

    strObject = "winmgmts://" & strComputer

    For Each Process In GetObject(strObject).InstancesOf("Win32_Process")
        If UCase(Process.Name) = UCase(strProcess) Then
            ... 'I do not know how to code this here for return instance of Process is running ...
            Exit Function
        End If
    Next
End Function

Function shell()
    If isProcessRunning(".", "cmd.exe") == False Then
        Dim objShell
        Set objShell = WScript.CreateObject("WScript.Shell")
        shellFn = objShell.Run("cmd")
    Else
        shellFn = getProcessRunning(".", "cmd.exe")
    End If
End Function

Dim cmdExe
cmdExe = shell
cmdExe "shutdown -s -t 60"

Because cmd.exe is already running cmdExe "shutdown -s -t 60" will re-using cmd and execute command.

Upvotes: 2

Views: 734

Answers (1)

Hackoo
Hackoo

Reputation: 18827

Give a try for this example :

Option Explicit
Dim ProcessPath1,ProcessPath2
ProcessPath1 = "%windir%\system32\cmd.exe"
ProcessPath2 = "%ProgramFiles%\Internet Explorer\iexplore.exe"
Call CheckProcess(ProcessPath1)
Call CheckProcess(ProcessPath2)
'**************************************************************************
Sub CheckProcess(ProcessPath)
    Dim strComputer,objWMIService,colProcesses,WshShell,Tab,ProcessName
    strComputer = "."
    Tab = Split(ProcessPath,"\")
    ProcessName = Tab(UBound(Tab))
    ProcessName = Replace(ProcessName,Chr(34),"")
    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 DblQuote(ProcessPath)
    Else
        MsgBox DblQuote(ProcessName) & " is aleardy running !", vbExclamation,_
        DblQuote(ProcessName) & " is aleardy running !"
        Exit Sub
    End if
End Sub
'**************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************

Upvotes: 2

Related Questions