Close/Terminate Another Application In VB6

Here's two short examples showing how to request another application to close, and also how to forcefully terminate another running application from a VB6 application. Both methods utilise Windows API calls.


1. Request The Application To Close

This example sends the WM_CLOSE message to the application, requesting it to close. (See below if you want to forcefully terminte the application instead.)

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
	(ByVal lpClassName As String, _
	ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" _
	Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
	ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const WM_CLOSE As Long = &H10

Public Sub closeApp(ByVal sWindowTitle As String, _
	Optional ByVal fSilent As Boolean = False)

	' Send another application a request to close gracefully

	Dim lHwnd As Long

	On Error GoTo errError1

	' Get window handle of the desired application to close.
	lHwnd = FindWindow(vbNullString, sWindowTitle)

	If lHwnd = 0 Then
		If Not fSilent Then
			Err.Raise 1, , "Can't find application to close"
		End If
	Else
		' Send the application the WM_CLOSE message.
		PostMessage lHwnd, WM_CLOSE, 0, 0
	End If

	Exit Sub

errError1:
	Err.Raise Err.number, , Err.description
End Sub
			

Now just call the closeApp sub specifying the Window Title of the application you want to close. Optionally, specify whether to raise an error if we can't find the window handle.


2. Forcefully Terminate Another Running Application

Use this method to forcefully terminate the other running process. This will kill the application and won't give it a chance to do it's shutting down processes, like saving settings or clearing up.

Public 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, ByRef lpdwProcessId 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 CloseHandle Lib "Kernel32" _
    (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "Kernel32" _
    (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_TERMINATE As Long = &H1

Public Sub terminateApp(ByVal sWindowTitle As String, ByVal fSilent As Boolean)

    ' Forcefully terminate a running application
    
    Dim lHwnd As Long
    Dim lProc As Long
    Dim lProcHnd As Long
    
    On Error GoTo errError1
    
    sWindowTitle = "Inbox - Thunderbird"
    sWindowTitle = "Test"
    
    ' Get the target's window handle.
    lHwnd = FindWindow(vbNullString, sWindowTitle)
    If lHwnd = 0 Then
        If fSilent Then
            Exit Sub
        Else
            Err.Raise 1, , "Can't find window handle of application to terminate"
        End If
    End If

    ' Get the process
    GetWindowThreadProcessId lHwnd, lProc
    If lProc = 0 Then
        If fSilent Then
            Exit Sub
        Else
            Err.Raise 1, , "Can't get process ID of window handle"
        End If
    End If

    lProcHnd = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0, lProc)
    If lProcHnd = 0 Then
        If fSilent Then
            Exit Sub
        Else
            Err.Raise 1, , "Can't get process handle"
        End If
    End If

    ' Terminate Process
    If TerminateProcess(lProcHnd, 0&) <> 0 Then
        If Not fSilent Then
            Err.Raise 1, , "Failed to terminate process"
        End If
    End If

    ' Close the process.
    CloseHandle lProcHnd
    
    Exit Sub

errError1:
    Err.Raise Err.number, , Err.description
End Sub
			

Again, just call the terminateApp sub specifying the Window Title of the application you want to close. Optionally, specify whether to raise an error if we can't find the window handle or other errors.

Page Updated 15/01/10