Library code snippets

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

'THIS WILL CLOSE AN APP BY CAPTION ONLY

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
EVT:
Err.Clear
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.

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Close Application by Caption.

Leave a comment

Sign in or Join us (it's free).

Mike J
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback