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:
- I have just opened
cmd.exe. - 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.
objShell.Run "shutdown -s -t 60").