Click to See Complete Forum and Search --> : Killing an Application


Reznor_X
January 31st, 2000, 02:26 PM
Is it possible to kill an app from vb just like the Ctrl+Alt+Del->End Task does? if is, how should I do it?

The app I want to kill isn't started from my program, so, I haven't used shell or something like that. Also, I need something that forces and not request the process to end.

Thanks in advance

January 31st, 2000, 04:00 PM
Make a search for "Closing running program" or something like that on this forum.
Vlad

Aaron Young
January 31st, 2000, 10:21 PM
Try this Sub I Wrote:
private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (byval lpClassName as string, byval lpWindowName as string) as Long
private Declare Function GetWindowThreadProcessId Lib "user32" (byval hwnd as Long, lpdwProcessId as Long) as Long
private Declare Function GetExitCodeProcess Lib "kernel32" (byval hProcess as Long, lpExitCode as Long) as Long
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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, lParam as Any) as Long

private Const PROCESS_TERMINATE = &H1
private Const WM_CLOSE = &H10

public Sub ShutDownApp(optional byval WindowCaption as string, optional byval Class as string, optional byval Force as Boolean = false)
Dim lHwnd as Long
Dim lProcID as Long
Dim lExitCode as Long
Dim lProcHnd as Long

lHwnd = FindWindow(Class, WindowCaption)
If lHwnd then
If Force then
'Force the Application to Terminate
Call GetWindowThreadProcessId(lHwnd, lProcID)
lProcHnd = OpenProcess(PROCESS_TERMINATE, 0&, lProcID)
Call GetExitCodeProcess(lProcHnd, lExitCode)
Call TerminateProcess(lProcHnd, lExitCode)
else
'Ask the Application to Close
Call SendMessage(lHwnd, WM_CLOSE, 0&, 0&)
End If
else
MsgBox "Window/Application not Found", vbExclamation + vbOKOnly, "Aborted"
End If
End Sub



Usage: ShutDownApp([WindowCaption], [WindowClass], [Force])

[b]Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com