DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

How to close external programs?

Hi there,
in vbs is a command "ExtProgram(ExtProgramName, ExtProgramArg)" to start external programs. But how to  close external programs when they finished its work? Is there a vbs-command for it? Is there a way when calling "cmd.exe" with a proper parameter?

Martin Bohm

0 Kudos
Message 1 of 5
(5,185 Views)
Hello Martin!

For a 'good' solution you have to call the external program in a way that it will exit after finishing work. Another way - if implemented - is to start the external program as a OLE server.

If this isn't possible you have to be more violent. On Windows XP there is a small tool 'taskkill.exe'. Call this program with parameters wich will identify the external program (e.g. Programm name) and it will be aborted (no common exit!). Note I: You have to be shure that the work was already finished. Note II: This will abort all runing instances of this program!

Example 'taskkill.exe' call to 'exit' DIAdem:
C:\WINDOWS\system32\taskkill.exe /F /IM DIAdem.exe
See M$ help for parameter usage or use -? parameter

Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 5
(5,179 Views)
Thank you for the hint, it helps.
Is there a command to check if a certain program is still running?

Martin Bohm
0 Kudos
Message 3 of 5
(5,179 Views)
Hello Martin!

Yes, with the tool 'tasklist' in the same directory.

The problem is that it is not easy to query from script. I tried it in the following script. Perhaps not perfect espacially because a command interpreter pop up every time you call the function.
Option Explicit
 
 
If
IsDIAdemRunning() Then
Call
MsgBox(
"At least one DIAdem is running!" )
Else
Call
MsgBox(
"No DIAdem at all!" )
End If
 
 
Function
IsDIAdemRunning()
Dim oWshShell
Dim oExec
 
' Execute via Shell Object
Set oWshShell = CreateObject("WScript.Shell")
 
Set oExec = oWshShell.Exec(
"C:\windows\system32\tasklist.exe /FI ""IMAGENAME eq DIAdem.exe""")
 
' wait until tasklist is finished
Do While oExec.Status = 0
Pause(1)
Loop
 
' no Standard Output -> no DIAdem running
IsDIAdemRunning = Len(oExec.StdOut.ReadAll) <> 0
End Function
A better solution might be possible via WMI.

Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
Message 4 of 5
(5,163 Views)
Hello Martin!

Now I had the time to look for the solution with WMI. You have to be shure that it is allowed/possible to use WMI on the target computer. This solution is faster and more reliable compared to the taskkill/tasklist solutions.

The script shows how to get the processlist for a specific EXE. It prints the count wich you can use to find out if a process is running and then termiante all running instances of this EXE.
Option Explicit
 
Dim
oWMI
Dim oProcessList
Dim oProcess
 
' Get WMI for the local computer
Set oWmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
 
' Query process list
Set oProcessList = oWmi.ExecQuery("Select * from Win32_Process Where Name = 'DIAdem.exe'")
 
' Print running processes count
Call MsgBox( oProcessList.Count & " DIAdem.exe instances are running")
 
' Terminate all runing processes
For Each oProcess in oProcessList
oProcess.Terminate()
Next
Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 5 of 5
(5,156 Views)