'*************************************************************
'Passed a partial or complete window title, forcefully kills it
'*************************************************************
Public Function KillProcess(PartialTitle As String)
plngWndHwnd = GetWindowFromTitle(PartialTitle)
plngRtn = GetWindowThreadProcessId(plngWndHwnd, plngPID)
If plngPID > 0 Then
plngOpenProcHwnd = OpenProcess(PROCESS_ALL_ACCESS, 0&, plngPID)
If plngOpenProcHwnd <> 0 Then
KillWindow plngWndHwnd, plngOpenProcHwnd
plngRtn = CloseHandle(plngOpenProcHwnd)
End If
End If
End Function
'****************************************************
' Passed a partial or complete window title, return
' the handle of the first title found matching
' the passed string.
' NOTE: Case insensitive
'****************************************************
Public Function GetWindowFromTitle(xstrPartialTitle As String) As Long
Dim pstrTitle As String * 256
Dim plngHwnd As Long
Dim plngRtn As Long
Dim pstrMatch As String
Dim pstrPassed As String
GetWindowFromTitle = 0
'Make sure passed string is uppercase
pstrPassed = UCase$(xstrPartialTitle)
'The desktop is the highest window
plngHwnd = GetDesktopWindow()
'It's first child is the 1st top level window
plngHwnd = GetWindow(plngHwnd, GW_CHILD)
'Get the text of the window
plngRtn = GetWindowText(plngHwnd, pstrTitle, Len(pstrTitle))
On Error Resume Next
If plngRtn = 0 Then
pstrMatch = ""
Else
pstrMatch = UCase$(Mid$(pstrTitle, 1, plngRtn))
End If
'Check to see if first child contains the passed string
If Len(pstrMatch) > 0 Then
If InStr(pstrPassed, pstrMatch) > 0 Then
GetWindowFromTitle = plngHwnd
Exit Function
End If
End If
On Error GoTo 0
'Now check all the top level windows
Do
plngHwnd = GetWindow(plngHwnd, GW_HWNDNEXT)
'Get the text of the window
pstrTitle = ""
plngRtn = GetWindowText(plngHwnd, pstrTitle, Len(pstrTitle))
On Error Resume Next
If plngRtn = 0 Then
pstrMatch = ""
Else
pstrMatch = UCase$(Mid$(pstrTitle, 1, plngRtn))
End If
'Check to see if this window contains the passed string
If Len(pstrMatch) > 0 Then
If InStr(pstrMatch, pstrPassed) > 0 Then
GetWindowFromTitle = plngHwnd
Exit Function
End If
End If
On Error GoTo 0
Loop While plngHwnd <> 0
GetWindowFromTitle = 0
End Function
'******************************************************
' Passed a Windows handle and a Process handle, kill it
'******************************************************
Public Sub KillWindow(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 & ", " & _
ReturnApiErrString(Err.LastDllError)
Else
'MsgBox "Destroyed window!"
End If
End If
End If
End Sub