CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Location
    MX
    Posts
    51

    Killing an Application

    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



  2. #2
    Guest

    Re: Killing an Application

    Make a search for "Closing running program" or something like that on this forum.
    Vlad


  3. #3
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Killing an Application

    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: [b]ShutDownApp([WindowCaption], [WindowClass], [Force])

    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured