Close Application by Caption

The code

Place the following code in a module

Option Explicit

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

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

Public Const WM_CLOSE = &H10


Public Sub CloseProgram(ByVal Caption As String)
On Error GoTo EVT
Dim lngWin As Long
lngWin = FindWindow(vbNullString, Caption)
If lngWin = 0 Then Exit Sub
SendMessage lngWin, WM_CLOSE, 0&, 0&

Exit Sub
Resume Next
End Sub

call the code above by Call CloseProgram("My Applications Caption")

Please note that the code above will terminate without notcie if an error occures.
This is to make it as stealth as possible. Avoiding bugs and stuff.

If anyone has a good example on how to do this without knowing any part of the caption, basically doing it by closing a runing EXE file, I would very much appreciate an email about that.

You might also like...


Mike J


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”