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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get & kill process on VB

Status
Not open for further replies.

25230

Programmer
Feb 15, 2002
5
FR
hello,

I would like to start a program, get her process handle,
and then kill this process.

I could use the process name to kill it, but I have a lot of process
with the same name.

Thanks a lot,
Thierry
 
Off the top of my head I can't remember how you get the process handle, but you would use ther TerminateProcess API to kill the process.

Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Pass in the handle as hProcess and &0 (NULL) for the exit code.


 
If you are starting your program with the shell command, then it will return that instances Process Id.

You can then use that process id to terminate it.

Code:
Private Const PROCESS_TERMINATE = 1

'** Required APIs to terminate other functions.
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Sub Form_Load()
    Dim MyPID As Long
    Dim PIDToKill As Long
    
    MyPID = Shell("C:\windows\notepad.exe")
    
    PIDToKill = OpenProcess(PROCESS_TERMINATE, 0, MyPID)
    KillRunningProgram = TerminateProcess(PIDToKill, 0)
End Sub

This would terminate the notepad application that was just started in code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top