Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

End a specific process 4

Status
Not open for further replies.

Bubbler

IS-IT--Management
Dec 14, 2003
135
US
Lets say there is a process running called myprocess.exe that was launched by my app (not in the applications window, in the processes window) Can I check for and kill it on exit of my app? I say check for, because it may or may not be running depending on user input. And can it be done across (x, 2000 XP etc?

 

Bubbler, a quick search in your forums for any date using all words on the words of process you would have found the following threads on the first page of the results.

Killing Processes thread222-730420

Find a Process not a window. thread222-460991

Stop applications from running thread222-716715

How to close a program thru code thread222-77760

Kill process using PID thread222-683642

and if you would have done the whole site this would have come up somewhere also

Ending Unmanaged Processes thread796-715271

Good Luck

 
This is easy if you launch the external process myprocess.exe from Shell function.

See the following code. Place a command button on the form and run the following code.
___
[tt]
Option Explicit
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const PROCESS_TERMINATE As Long = &H1&
Dim idProcess As Long
Private Sub Command1_Click()
On Error Resume Next
AppActivate idProcess
If Err Then idProcess = Shell("calc.exe", vbNormalFocus)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, 0, idProcess)
TerminateProcess hProcess, 0
CloseHandle hProcess
End Sub[/tt]
___

Clicking the button will start Calculator application. If you close your program, Calculator will also close if it is running.
 
Hi...
I tried your method Hypetia, and it works great.
I want to be able to start and end the external program from my main program. However, if I close the main program, I want the external program to continue running.
I added another command button to the form and replaced your "Private Sub Form_Unload(Cancel As Integer)" with
"Private sub Command2_Click()".
The main program starts and stops the external program, and the external program continues to run when I close the main program.
My problem is that when I re-open the main program, it will not stop the external program. I can restart another instance of the external program and end it, but the first instance of the external program can not be stopped from the re-opened main program.
Why is this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top