API/function doesn't stop and I don't understand it :o) 
Author Message
 API/function doesn't stop and I don't understand it :o)

Hi All,

I'm trying to write some code to close all windows with a certain
title. Somehow I'm doing something wrong with the hwnd.  The code I'm

Private Sub CloseApps(ByRef strVar1 As String, ByRef strVar2 As String)
'StrVar1 contains All or One. Incase of One just the first found
'window with this title is closed
'In case of All all windows must be closed
'StrVar2 contains the title of the window I want to close, including
'the wildcards (for example "*Microsoft Internet Explorer")
Dim lHwnd As Long
Dim lretval

    If strVar2 = "All" Then
        'Close all instances
        lHwnd = 1

        Do While lHwnd <> 0
            'Get the windows, using wildcards
            lHwnd = FindWindowWild(strVar1, False)
            Debug.Print lHwnd
            If lHwnd <> 0 Then
                lretval = PostMessage(lHwnd, WM_CLOSE, 0&, 0&)
            End If
        'Close just first instance
        lHwnd = FindWindowWild(strVar1, False)
        If lHwnd <> 0 Then
            lretval = PostMessage(lHwnd, WM_CLOSE, 0&, 0&)
        End If
    End If

End Sub

All the used API's/Functions are at the end of this message. Closing
just one window works fine. When using the 'All' option, all windows
are closed, but then the application get's into an infinitive loop. As
far as I can see in the function EnumWinProc. If I'm interpreting
things the right way: It looks like hwnd never gets 0, but after
closing the last window it keeps the value of the last window.

What am I missing here ???????

Thanks for your time,

Gerard Verbruggen

'Used to close a window by its name
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
As Long

Public Const WM_CLOSE = &H10

Private Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As
Long, ByVal lParam 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 IsWindowVisible& Lib "user32" (ByVal hwnd As

Private Declare Function GetParent& Lib "user32" (ByVal hwnd As Long)
    Dim sPattern As String, hFind As Long

Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim k As Long, sName As String

    If IsWindowVisible(hwnd) And GetParent(hwnd) = 0 Then
        sName = Space$(128)
        k = GetWindowText(hwnd, sName, 128)

        If k > 0 Then
            sName = Left$(sName, k)
            If lParam = 0 Then sName = UCase(sName)

            If sName Like sPattern Then
                hFind = hwnd

EnumWinProc = 0
    Exit Function
End If
End If
End If

EnumWinProc = 1
End Function

Public Function FindWindowWild(sWild As String, Optional bMatchCase As
Boolean = True) As Long
    sPattern = sWild
    If Not bMatchCase Then sPattern = UCase(sPattern)

    EnumWindows AddressOf EnumWinProc, bMatchCase
    FindWindowWild = hFind
End Function

Sent via Deja.com http://www.*-*-*.com/
Before you buy.

Mon, 24 Feb 2003 01:24:58 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Don't understand 'Set'

2. Access 97 doesn't understand VBA functions

3. Win API functions that don't seem to exist in Win32 API

4. Don't understand dim statements

5. Help - Don't understand anchors.

6. ADO: I don't understand...

7. I still don't understand......

8. ADO: I don't understand...

9. Don't understand this error

10. Please help, I don't understand this!!!

11. I don't understand....Is this a bug of WSH

12. H E L P or don't you understand my cuestion


Powered by phpBB® Forum Software