To close an application, once you have the hwnd, use this code:
VB Code:
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 SC_CLOSE = &HF060& Private Const WM_SYSCOMMAND = &H112 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 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 Sub CloseWindowByHwnd(ByVal hwnd&) 'Nucleus Dim lPid As Long Dim lHp As Long SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0 ' ' If the window doesn't like gentle persuasion, bring out the nipple clamps to force it to close ' If IsWindow(hwnd) Then Call GetWindowThreadProcessId(hwnd, lPid) lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid) TerminateProcess lHp&, 0& CloseHandle lHp End If ' End Sub




Reply With Quote