Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's difference between GetWindow and FindWindow? 1

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
Hi,

I've found the following code to cycle through all the open windows and retrieve the class name for each window, I want to find all Internet Explorer windows (they have a class name of "IEFrame") and close them (it's the basis for a popup-defender I'm writing).

I can't figure out why if I pass the title for an IE window to FindWindow it returns a different handle to when I use GetWindow. I don't want to use FindWindow because if I have 3 IE windows open all at the same page it won't differentiate between them.

Can I use GetWindow to return the handle that I'm after?

lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)

'Loop while there are still open windows.
Do While lngHand <> 0
GetWindowText lngHand, strName, Len(strName)
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
If (Left$(strName, 1) <> vbNullChar) Then
' Is it an Internet Explorer window?
GetClassName lngHand, strName, Len(strName)
If (Left$(strName, 1) <> vbNullChar) Then
If (Left$(strName, InStr(strName, vbNullChar) - 1) = &quot;IEFrame&quot;) Then
' Never gets to this line!!!!! Why???
lngRtn = GetWindowThreadProcessId(lngHand, lngProcID)
lngProc = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), lngProcID)
lngRtn = TerminateProcess(lngProc, CLng(0))
End If
End If
End If
Loop


- Andy
_______________________________
&quot;On a clear disk you can seek forever&quot;
 
There are a number of problems with the code, not least of which is that even if it was working as expected, you will be skipping over every other window. Here's a cleaned up version (although you are probably better off using EnumWindows because GetWindow can allegedly run into problems if used in a loop):
[tt]
Option Explicit

Private Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long
Private Declare Function GetWindow Lib &quot;user32&quot; (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowTextLength Lib &quot;user32&quot; Alias &quot;GetWindowTextLengthA&quot; (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib &quot;user32&quot; Alias &quot;GetWindowTextA&quot; (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib &quot;user32&quot; Alias &quot;GetClassNameA&quot; (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Sub Command1_Click()
FindAndKillIE
End Sub

Private Sub FindAndKillIE()
Dim strName As String ' Without this declaration strName is a Variant, and GetClassName won't work the way you expect
Dim lngHand As Long
Dim result As Long

lngHand = GetWindow(GetDesktopWindow(), GW_CHILD)

'Loop while there are still open windows.
Do While lngHand <> 0
strName = String(255, Chr$(0)) ' Initialise string buffer
result = GetClassName(lngHand, strName, Len(strName)) ' grab class name
If result Then
' Is it an Internet Explorer window?
If Left(strName, result) = &quot;IEFrame&quot; Then
' For the sake of this cleaned up example we have commented out
' the process killing code, and put in a debug statement
Debug.Print lngHand, Left(strName, result)
' lngRtn = GetWindowThreadProcessId(lngHand, lngProcID)
' lngProc = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), lngProcID)
' lngRtn = TerminateProcess(lngProc, CLng(0))
End If
End If
lngHand = GetWindow(lngHand, GW_HWNDNEXT) ' Moved this to the right place to avoid skipping every other window
Loop

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top