Option ExplicitPrivate 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, pdwResult As Long) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long'Purpose : Terminates an application by finding the process ID of a windows handle.'Inputs : lHwnd The application window handle'Outputs : Returns True if succeeds'Notes : If you know the applications process ID then you need only call the last three lines of this routine.Function ApplicationTerminate (lHwnd As Long) As BooleanDim lPid As Long, lReturn As Long, lhwndProcess As LongConst PROCESS_ALL_ACCESS = &H1F0FFF'Get the PID (process ID) from the application handleLet lReturn = GetWindowThreadProcessId(lHwnd, lPid)'Terminate the applicationLet lhwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)Let ApplicationTerminate = (TerminateProcess(lhwndProcess, 0&) <> 0)Let lReturn = CloseHandle(lhwndProcess)End Function'Purpose : Tests the status of an application'Inputs : lHwnd The application window handle' [lWaitTimeOut] The time in ms to wait for the application to respond'Outputs : Returns True if application is responding, else returns' false if the application is not responding'Notes : SMTO_ABORTIFHUNG Returns without waiting for the time-out period to elapse if the receiving' process appears to be in a "hung" state.' SMTO_BLOCK Prevents the calling thread from processing any other requests until the function returns.Function ApplicationResponding (lHwnd As Long, Optional lWaitTimeOut As Long = 2000) As BooleanDim lResult As LongDim lReturn As LongConst SMTO_BLOCK = &H1, SMTO_ABORTIFHUNG = &H2, WM_NULL = &H0Let lReturn = SendMessageTimeout(lHwnd, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, lWaitTimeOut, lResult)If lReturn ThenLet ApplicationResponding = TrueElseLet ApplicationResponding = FalseEnd IfEnd Function
'Demonstration routineSub Test()Dim lHwnd As Long'Find an instance of internet explorer'I used IE to test it as it only takes about 2 mins before it hangs!Let lHwnd = FindWindow("IEFrame", vbNullString)If lHwnd ThenIf ApplicationResponding(lHwnd) = False Then'Application is not respondingIf ApplicationTerminate(lHwnd) = True ThenMsgBox "Successfully terminated application"End IfEnd IfEnd IfEnd Sub
References:
Tags: VBA, Tips, application, responding, stop, close, terminate
Tags: VBA, Tips, application, responding, stop, close, terminate