Results 1 to 4 of 4

Thread: Closing an application forcefully

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Singapore
    Posts
    116

    Post

    How do we close a program forcefully without going through then Unload Procedure???

    ------------------
    YC Sim
    Teenage Programmer
    UIN 37903254


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

    Post

    End


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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Singapore
    Posts
    116

    Post

    I mean... an external program.

    ------------------
    YC Sim
    Teenage Programmer
    UIN 37903254


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

    Post

    In that case you can use the SendMessage API and the WM_CLOSE Message to tell an Application to Close, eg.
    Code:
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Sub Command1_Click()
        Dim tPoint As POINTAPI
        Dim lHwnd As Long
        
        Call GetCursorPos(tPoint)
        lHwnd = WindowFromPoint(tPoint.x, tPoint.y)
        Call SendMessage(lHwnd, WM_CLOSE, 0, 0)
    End Sub
    If that doesn't shut it down, you can use the TerminateProcess API, but be warned, this will Forcefully Shutdown whatever you pass it, no questions asked.
    Code:
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 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 Const PROCESS_ALL_ACCESS = &H1F0FFF
    
    Private Sub Command1_Click()
        Dim tPoint As POINTAPI
        Dim lHwnd As Long
        Dim lPID As Long
        Dim lCode As Long
        
        Call GetCursorPos(tPoint)
        lHwnd = WindowFromPoint(tPoint.x, tPoint.y)
        Call GetWindowThreadProcessId(lHwnd, lPID)
        lPID = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPID)
        Call GetExitCodeProcess(lPID, lCode)
        Call TerminateProcess(lPID, lCode)
    End Sub

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

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