End Process from Task Manager using VB 6 Code
Solution 1
There are two ways:
Send
WM_CLOSE
to the target application if it has a window (hidden/visible). Task Manager's "End Task" uses this method. Most of the applications handle WM_CLOSE and terminate gracefully.Use
TerminateProcess
API to kill forcefully - Task Manager's "End Process" uses this method. This API forcefully kills the process.
An example can be found here:
VB Helper: HowTo: Terminate a process immediately
Solution 2
Use vb6.0 TaskKill
Private Sub Command1_Click()
Shell "taskkill.exe /f /t /im Application.exe"
End Sub
Solution 3
Call ShellExecute with the TaskKill command
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } [/T] [/F]
Description: This tool is used to terminate tasks by process id (PID) or image name.
Solution 4
Shell "taskkill.exe /f /t /im processname.exe"
This forces (/f
) the terminatation of the process with the image name (/im
) of processname.exe, and any child processes which were started by it (/t
). You may not need all these switches. See the taskkill
command help for more information (type the following at the command line):
taskkill/?
Solution 5
Karl Peterson's excellent archive of VB6 code has high quality sample code and full explanations using both WM_CLOSE and TerminateProcess. Accept no substitutes!
One pitfall you might see in a lot of code out there is that sending WM_CLOSE to a single window handle you have isn't sufficient - most applications comprise numerous windows. The answer as implemented in Karl's code: Find all the top-level windows belonging to this application and send the message to each.
oliverwood
Updated on May 05, 2020Comments
-
oliverwood almost 4 years
I need to kill an application roughly so I can get phantom subscriber of that application in my database (this can not be produced by closing the application). Manually, if we kill the application from Task Manager, the phantom subscriber will be exist. Now I need to do it automatically in VB 6 code. Help! Thanks.
-
tmighty almost 2 yearsI don't think TaskManager uses TerminateProcess to end a process. I have run the code that you pointed to, and it did not terminate a SYSTEM process. Using the TaskManager, I could terminate it. So I don't think they work exactely the same.