Results 1 to 4 of 4

Thread: closing an app

  1. #1

    Thread Starter
    New Member
    Join Date
    May 1999
    Location
    Toronto, Ontario, Canada
    Posts
    7

    Post

    Ok, here is the code I'm using to display all the current tasks running on my computer...

    Private Sub List_Click()
    LoadTaskList
    End Sub
    Sub LoadTaskList()
    Dim CurrWnd As Long
    Dim Length As Long
    Dim TaskName As String
    Dim Parent As Long

    List1.Clear
    CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)

    While CurrWnd <> 0
    Parent = GetParent(CurrWnd)
    Length = GetWindowTextLength(CurrWnd)
    TaskName = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, TaskName, Length + 1)
    TaskName = Left$(TaskName, Len(TaskName) - 1)

    If Length > 0 Then
    If TaskName <> Me.Caption Then
    List1.AddItem TaskName
    End If
    End If
    CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    DoEvents
    Wend
    End Sub


    I want to be able to click on a command button and have it close the app or window that's selected in the Listbox. How do I do that, it may seem simple to you, but could someone please help me out.

    thanks

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    If you have the Window Handle, (Which you do), there are 2 Methods you can use; Close and Terminate. Close will work most of the Time, but for those more Stubourn Apps you may need to tell it to Terminate, here's a couple of Subs I put together you can call to do the job:

    In a Module..
    Code:
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode 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 WM_CLOSE = &H10
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF
    
    Public Sub ForceAppClosed(ByVal lHwnd As Long)
        'Force the App to Shutdown
        Dim lPID As Long
        Dim lCode As Long
        'Get the Windows ProcessID
        Call GetWindowThreadProcessId(lHwnd, lPID)
        'Get a Handle to the Process
        lPID = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPID)
        'Get the Processes Exit Code
        Call GetExitCodeProcess(lPID, lCode)
        'Tell the Process to Terminate
        Call TerminateProcess(lPID, lCode)
    End Sub
    
    Public Sub CloseApp(ByVal lHwnd As Long)
        'Ask the App to Shutdown Normally
        Call SendMessage(lHwnd, WM_CLOSE, 0, 0)
    End Sub
    Usage: CloseApp WindowHandle
    or.. ForceAppClosed WindowHandle

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

  3. #3
    Member
    Join Date
    Jan 1999
    Location
    Gig Harbor, WA, USA
    Posts
    48

    Post

    I would not recommend either of these solutions. Instead, use the following:

    Code:
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_CLOSE = &H10
    PostMessage (hWnd), WM_CLOSE, 0&, 0&
    Hope this helps!

    ------------------
    (¯`·.¸¸.·´¯`·->ShadowCrawler<-·´¯`·.¸¸.·´¯)
    Teenage Programmer
    Visual Basic, HTML, C++, JavaScript

    http://welcome.to/X12Tech
    Email: [email protected]
    ICQ#: 9872708

  4. #4

    Thread Starter
    New Member
    Join Date
    May 1999
    Location
    Toronto, Ontario, Canada
    Posts
    7

    Post

    Thanks to both of you guys, I think I'll try both methods and see which one seems to work better and/or is more efficient.

    Those responses were right quick, keep up the good work !

    : )

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