ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Closing a program (https://www.excelbanter.com/excel-programming/385475-closing-program.html)

MarkS

Closing a program
 
Hi,
I've never done this or seen anything on it, but i works it's simple
1. Test if program is runing
2. If not start program
3. at end of job shut program down

I can start the program using this
Shell ("C:\Program Files\Reuters\kobra\Program\kobra.exe")
But if its already running it start a second instantance and I don't know
how to get it to shut down

Thanks

Steve Yandl

Closing a program
 
The two subroutines below are examples of one approach. These will not work
on Win95/98 systems unless they've added WMI but they should work fine with
WinXP or Win2K.

The subroutine "RunOneNotepad" checks the list of running processes and
determines if any are named notepad.exe (I don't have kobra.exe so I tested
with notepad.exe which I do have). If none are found, it launches a new
process and documents the process ID ("PID") in cell A1 of Sheet1.

The subroutine "KillSpecificPID" extracts the PID from cell A1 (if there is
one there) and kills the process with that ID if it is still running.

___________________________________

Sub RunOneNotepad()
Dim runningNotepad As Boolean

runningNotepad = False

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcess
If objProcess.Name = "NOTEPAD.EXE" Then
runningNotepad = True
End If
Next

If runningNotepad = False Then
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2:W in32_Process")
objWMI.Create "notepad.exe", Null, Null, intProcessID
Sheets(1).Cells(1, 1).Value = intProcessID
End If

runningNotepad = False
Set objWMI = Nothing

End Sub



Sub KillSpecificPID()

intPID = Sheets(1).Cells(1, 1).Value

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process Where ProcessID = " & intPID & "")
For Each objProcess In colProcess
objProcess.Terminate
Next
End Sub



Steve




"MarkS" wrote in message
...
Hi,
I've never done this or seen anything on it, but i works it's simple
1. Test if program is runing
2. If not start program
3. at end of job shut program down

I can start the program using this
Shell ("C:\Program Files\Reuters\kobra\Program\kobra.exe")
But if its already running it start a second instantance and I don't know
how to get it to shut down

Thanks




MarkS

Closing a program
 
Hi,
this works fine, with a couple of obvious changes. A question that occured
to me what are the two null's for in objWMI.Create


"Steve Yandl" wrote:

The two subroutines below are examples of one approach. These will not work
on Win95/98 systems unless they've added WMI but they should work fine with
WinXP or Win2K.

The subroutine "RunOneNotepad" checks the list of running processes and
determines if any are named notepad.exe (I don't have kobra.exe so I tested
with notepad.exe which I do have). If none are found, it launches a new
process and documents the process ID ("PID") in cell A1 of Sheet1.

The subroutine "KillSpecificPID" extracts the PID from cell A1 (if there is
one there) and kills the process with that ID if it is still running.

___________________________________

Sub RunOneNotepad()
Dim runningNotepad As Boolean

runningNotepad = False

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcess
If objProcess.Name = "NOTEPAD.EXE" Then
runningNotepad = True
End If
Next

If runningNotepad = False Then
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2:W in32_Process")
objWMI.Create "notepad.exe", Null, Null, intProcessID
Sheets(1).Cells(1, 1).Value = intProcessID
End If

runningNotepad = False
Set objWMI = Nothing

End Sub



Sub KillSpecificPID()

intPID = Sheets(1).Cells(1, 1).Value

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process Where ProcessID = " & intPID & "")
For Each objProcess In colProcess
objProcess.Terminate
Next
End Sub



Steve




"MarkS" wrote in message
...
Hi,
I've never done this or seen anything on it, but i works it's simple
1. Test if program is runing
2. If not start program
3. at end of job shut program down

I can start the program using this
Shell ("C:\Program Files\Reuters\kobra\Program\kobra.exe")
But if its already running it start a second instantance and I don't know
how to get it to shut down

Thanks





Steve Yandl

Closing a program
 
To be honest, I don't know. They're startup parameters not needed for
anything I've ever done. You may find some info he
http://msdn2.microsoft.com/en-us/library/aa394375.aspx
if you want to explore.

Steve




"MarkS" wrote in message
...
Hi,
this works fine, with a couple of obvious changes. A question that occured
to me what are the two null's for in objWMI.Create


"Steve Yandl" wrote:

The two subroutines below are examples of one approach. These will not
work
on Win95/98 systems unless they've added WMI but they should work fine
with
WinXP or Win2K.

The subroutine "RunOneNotepad" checks the list of running processes and
determines if any are named notepad.exe (I don't have kobra.exe so I
tested
with notepad.exe which I do have). If none are found, it launches a new
process and documents the process ID ("PID") in cell A1 of Sheet1.

The subroutine "KillSpecificPID" extracts the PID from cell A1 (if there
is
one there) and kills the process with that ID if it is still running.

___________________________________

Sub RunOneNotepad()
Dim runningNotepad As Boolean

runningNotepad = False

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcess
If objProcess.Name = "NOTEPAD.EXE" Then
runningNotepad = True
End If
Next

If runningNotepad = False Then
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2:W in32_Process")
objWMI.Create "notepad.exe", Null, Null, intProcessID
Sheets(1).Cells(1, 1).Value = intProcessID
End If

runningNotepad = False
Set objWMI = Nothing

End Sub



Sub KillSpecificPID()

intPID = Sheets(1).Cells(1, 1).Value

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process Where ProcessID = " & intPID & "")
For Each objProcess In colProcess
objProcess.Terminate
Next
End Sub



Steve




"MarkS" wrote in message
...
Hi,
I've never done this or seen anything on it, but i works it's simple
1. Test if program is runing
2. If not start program
3. at end of job shut program down

I can start the program using this
Shell ("C:\Program Files\Reuters\kobra\Program\kobra.exe")
But if its already running it start a second instantance and I don't
know
how to get it to shut down

Thanks








All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com