I want to be able to detect how many instances of Internet Explorer are running on a computer. For instance one window navigates the web and if another window pops up I want to detect that another instance of Internet Explorer is open and use this code to close it:
If there is an easier way to do what I'm attempting to do, which is only have one instance of Internet Explorer open at one time and close all the other windows that may pop up then I am open for suggestions.Code: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 Command1_Click() Dim lHwnd As Long Dim lProcess As Long Dim lExitCode As Long '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) End Sub
Thanks,
Publius




Reply With Quote