2

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.

7
  • 2
    You could use similar code used in isProcessRunning function to get Process object and manipulate/inspect object with properties and functions described here Commented Dec 12, 2017 at 4:34
  • Thank you for helping me, I tried to read your suggestions website, but I still not find solved my problem. If you have another suggest, please help me :D, Thank you again Commented Dec 12, 2017 at 9:57
  • @JoSerra I have just updated my question, please check it help me Commented Dec 12, 2017 at 9:59
  • Don't do what you're trying to do. You don't really gain anything from it anyway. Just run the command right away (objShell.Run "shutdown -s -t 60"). Commented Dec 12, 2017 at 10:31
  • @AnsgarWiechers I have a idea for writing a file execute a lot of command (auto to do something such as, start up a program, check network or disconnect, shutdown my computer with schedule ....), and file laucher.vbs can help me re-use cmd.exe. Besides I think I can re-use any process like singleton pattern here. Do you think it's possible? Commented Dec 12, 2017 at 10:48

1 Answer 1

2

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 '************************************************************************** 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I have just updated my question above. I want to get instance of process already running. May you help me, please
To return an object from a VBScript function you can assign object to function name with set command : Set getProcessRunning = Process. To receive object you must use set command again: set shellFn = getProcessRunning(".", "cmd.exe"). Otherwise, it has no sense to "reuse" cmd.exe process to execute shutdown command. You could execute shutdown command directly. Regards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.