Option Explicit
Private 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
Private Const SMTO_BLOCK = &H1
Private Const SMTO_ABORTIFHUNG = &H2
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112
Private Const WM_NULL = &H0
Private Declare Function IsWindowVisible& Lib "user32" (ByVal hwnd As Long)
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private 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
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private m_hTaskBar As Long
Private m_hDeskTopIcons As Long
Public Sub CloseExceptUs()
';->';->';->';->';->';->';->';->';->';->';->';->';->';->
' Author: Nucleus *
' Location: VB World Forum *
' Purpose: Close all applications except this one *
';->';->';->';->';->';->';->';->';->';->';->';->';->';->
m_hDeskTopIcons = FindWindowEx(0&, 0&, "Progman", vbNullString)
m_hTaskBar = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
EnumWindows AddressOf EnumWindowsProc, 0&
End Sub
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 the window is not desktop icons or taskabar
'
If hwnd <> m_hTaskBar And hwnd <> m_hDeskTopIcons Then
'
' Get ThreadID and Process Id from hwnd
'
lThreadID = GetWindowThreadProcessId(hwnd, lPid)
'
' If the ThreadId is not from this application
'
If lThreadID <> App.ThreadID Then
'
' Check if the window is visible
'
If IsWindowVisible(hwnd) Then
'
' Tell the window to close gently, give it a timeout in case it does not respond
'
SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
'
' If the window doesn't close via gently persuasion, bring out the nipple screws to force it to close
'
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