Sunday, June 17, 2012

Menutup Sebuah Aplikasi Secara Request

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)
Dim lHwnd As Long
On Error GoTo ErrHandler

lHwnd = FindWindow(vbNullString, sWindowTitle)

If lHwnd = 0 Then
If Not fSilent Then
Err.Raise 1, , "Can"
End If
Else
PostMessage lHwnd, WM_CLOSE, 0, 0
End If

Exit Sub

ErrHandler:
Err.Raise Err.Number, , Err.Description
End Sub