Saturday, April 3, 2010

VB6 Code - Menutup Seluruh Aplikasi Yang Sedang Berjalan

Di bawah ini merupakan fungsi VB6 untuk menutup seluruh aplikasi yang sedang berjalan. Mengapa seluruh aplikasi yang sedang berjalan tersebut harus ditutup? contoh kecilnya dalam pembuatan billing warnet. Misalnya A (user) log-out, kemudian datang B (user baru) log-in, B tidak akan melihat aplikasi-aplikasi yang masih terbuka (kemungkinan lupa ditutup oleh A), karena seluruh aplikasi yang sedang berjalan telah ditutup secara otomatis dengan fungsi di bawah ini.
Mungkin ada pertanyaan, Apakah ditutup dengan software billing warnetnya juga? ya, boleh jika kita mau, bahkan sekalian di shutdown pula.
Option Explicit

Public Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, _
ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Public Declare Function IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const SMTO_BLOCK = &H1
Public Const SMTO_ABORTIFHUNG = &H2
Public Const SC_CLOSE = &HF060&
Public Const WM_SYSCOMMAND = &H112
Public Const WM_NULL = &H0
Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Public HWND_Taskbar As Long
Public HWND_Desktop As Long
Public HWND_ExplorerW As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long

Dim lThreadID As Long
Dim lPid As Long
Dim lHp As Long

If hwnd <> HWND_Taskbar And hwnd <> HWND_Desktop And hwnd <> HWND_ExplorerW Then
lThreadID = GetWindowThreadProcessId(hwnd, lPid)
If lThreadID <> App.ThreadID Then
If IsWindowVisible(hwnd) Then
SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
If IsWindow(hwnd) Then
lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
TerminateProcess lHp&, 0&
CloseHandle lHp
End If
End If
End If
End If

EnumWindowsProc = 1

End Function

Public Sub CloseAllRuning()
HWND_Desktop = FindWindowEx(0&, 0&, "Progman", vbNullString)
HWND_Taskbar = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
Contoh Penggunaan fungsi untuk menutup seluruh aplikasi menggunakan VB6:
Sub Main()
Call CloseAllRuning
End Sub