Option Explicit
Private Const PROCESS_ALL_ACCESS = &H1F0FFF ' ------------------------------------------------- ' API's used to get window info ' ------------------------------------------------- Private Declare Function DestroyWindow _ Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function IsWindow _ Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function OpenProcess _ Lib "kernel32" _ (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle _ Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function TerminateProcess _ Lib "kernel32" _ (ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Private Declare Function GetWindowThreadProcessId _ Lib "user32" _ (ByVal hwnd As Long, _ lpdwProcessId As Long) As Long Public Sub KillWindow(ByVal xlngWndHwnd As Long) Dim plngPID As Long Dim plngRtn As Long Dim plngOpenProcHwnd As Long plngRtn = GetWindowThreadProcessId(xlngWndHwnd, plngPID) If plngPID > 0 Then plngOpenProcHwnd = OpenProcess(PROCESS_ALL_ACCESS, 0&, plngPID) If plngOpenProcHwnd <> 0 Then DoIt xlngWndHwnd, plngOpenProcHwnd plngRtn = CloseHandle(plngOpenProcHwnd) End If End If End Sub Private Sub DoIt(xlngHwnd As Long, xlngProcessHwnd As Long) Dim plngRtn As Long Dim plngProcessHwnd As Long Dim plngThreadHwnd As Long Dim plngExitCode As Long plngExitCode = 0& If xlngHwnd <> 0 Then If IsWindow(xlngHwnd) <> 0 Then plngRtn = TerminateProcess _ (xlngProcessHwnd, _ plngExitCode) If plngRtn = 0 Then MsgBox "Failed To destroy window: Error #" & _ Err.LastDllError & ", " & _ Err.LastDllError Else MsgBox "Destroyed window!" End If End If End If End Sub |