-
This code is really starting to frustrate me, LoL. For some reason when I put it in a form all by itself and open up two instance of Internet Explorer and then push the command button it closes the last Internet Explorer opened and leaves the other one. However, when I put that code in my program it closes all instances of Internet Explorer. Here is my code:
Code:
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Sub Timer1_Timer()
Dim lngIE As Long
Dim strBuffer As String
Dim lngLength As Long
Dim intCount As Integer
Dim lHwnd As Long
Dim lProcess As Long
Dim lExitCode As Long
Dim p As Integer
Do
lngIE = FindWindowEx(0, lngIE, "IEFrame", vbNullString)
lngLength = GetWindowTextLength(lngIE)
strBuffer = Space(lngLength)
Call GetWindowText(lngIE, strBuffer, Len(strBuffer))
If Len(Trim(strBuffer)) > 0 Then
intCount = intCount + 1
End If
Loop Until lngIE = 0
If intCount > 1 Then
For p = 1 To (intCount - 1)
'Get the Window Handle
lHwnd = FindWindow("IEFrame", vbNullString)
'Get the ProcessID
Call GetWindowThreadProcessId(lHwnd, lProcess)
'Get the Process Handle
lProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, lProcess)
'Get the Exitcode
Call GetExitCodeProcess(lProcess, lExitCode)
'Terminate the Process
Call TerminateProcess(lProcess, lExitCode)
Next p
End If
End Sub
Any help would be a helpful, LoL. I thank you in advance.
Publius
-
You're closing the whole process, not just the one thread which is what you want. Try sending a WM_CLOSE to the window you want closing instead...
Code:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Const WM_CLOSE = &H10
' your current code here to locate the window
' replace your close process code with...
Call SendMessage(lHwnd, WM_CLOSE, 0, 0)
I've used this to close child windows in a foreign MDI form and I see no reason why it shouldn't work for what you want it to.