Find and Close an Application

in Visual Basic®

by Rick Meyer

Home

This small project demonstrates how to search through all the apps currently running by window caption. If a match to your partial search string is found then that app is terminated.

If you experiment with this demo with your own search strings, be sure to check the task manager [Ctrl][Alt][Delete] to be sure that you do not terminate a vital windows program.

    Instructions for Building this Project:

1. Start a new standard exe.
2. On Form1 put a Textbox named Text1.
3. On Form1 put a CommandButton named Command1.

Now you are ready for the code. Select all of the following code (by clicking on the word 'Option' three times) and copy it to the clipboard [Ctrl][Insert]. Then paste it into the code window of Form1 with [Shift][Insert]. Variable Key
% As Integer
& As Long
! As Single
# As Double
$ As String
When this program is run, it automatically runs (Shells) Windows Notepad and places "Note" as a partial search string in the textbox. Click the command button to terminate "Notepad"
Option Explicit

'========================================================
'                  API Declarations
'========================================================
Const GW_HWNDFIRST& = 0
Const GW_HWNDNEXT& = 2
Const GW_OWNER& = 4

Private Declare Function GetWindow _
    Lib "user32" ( _
        ByVal hwnd As Long, _
        ByVal wCmd As Long) As Long
    
Const WS_DISABLED& = &H8000000
Const GWL_STYLE& = -16

Private Declare Function GetWindowLong _
    Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long

Const WS_CANCELMODE& = &H1F
Const WM_CLOSE& = &H10

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 Declare Function GetWindowText _
    Lib "user32" Alias "GetWindowTextA" ( _
        ByVal hwnd As Long, _
        ByVal lpString As String, _
        ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength _
    Lib "user32" Alias "GetWindowTextLengthA" ( _
        ByVal hwnd As Long) As Long

Private Declare Function IsWindow _
    Lib "user32" (ByVal hwnd As Long) As Long


'========================================================
'               Program Procedures
'========================================================
Sub FindandCloseApp(ByVal partialCaption$)
    Dim Whnd&, L&, Nam$

    partialCaption = LCase$(partialCaption)
    Whnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
    
    Do While Whnd <> 0
        If IsWindow(Whnd) Then
            L = GetWindowTextLength(Whnd)
            If L > 0 Then
                Nam = Space$(L + 1)
                L = GetWindowText(Whnd, Nam, L + 1)
                Nam = LCase$(Left$(Nam, L))

                If InStr(Nam, partialCaption) Then
                    EndTask Whnd
                    Exit Do
                End If
            End If
        End If
        Whnd = GetWindow(Whnd, GW_HWNDNEXT)
        DoEvents
    Loop
End Sub

Private Sub EndTask(Whnd As Long)
    'Do not close this window
    If Whnd = Form1.hwnd Then Exit Sub
    
    'Do not close this window's child
    ' (Do it without API)
    If GetWindow(Whnd, GW_OWNER) _
            = Form1.hwnd Then Exit Sub
    
    'Do not close if disabled
    If (GetWindowLong(Whnd, GWL_STYLE) _
        And WS_DISABLED) Then Exit Sub
    
    PostMessage Whnd, WS_CANCELMODE, 0&, 0&
    PostMessage Whnd, WM_CLOSE, 0&, 0&
End Sub

Private Sub Command1_Click()
    FindandCloseApp Text1.Text
End Sub

Private Sub Form_Activate()
    Shell "Notepad.exe"
    Text1 = "Note"
End Sub