Checking for the existence of another window.

Sorry this took so long to get back.  I wasn't able find the original
message so hopefully the person looking for this information will be
able to find it.

I used the code below in a VisualBASIC program to check if another
application was already running.  It uses the main windows title.  There
are some constants and a WinAPI declaration as well as the function
itself.  It shouldn't be too hard to port it to Delphi.

** Constants ***
Global Const GW_CHILD = 0
Global Const GW_HWNDFIRST = 1
' Global Const GW_HWNDLAST = ?
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4

** WinAPI declaration **
Declare Function GetWindow Lib "User" (ByVal hWnd As Integer, ByVal wCmd
As Integer) As Integer
Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal
lpString As String, ByVal nMaxCount As Integer) As Integer

** Actual Function **
Function FindWindow (sWindowText As String) As Integer
Dim sCaptionText As String
Dim iWndPtr As Integer
Dim iFound As Integer
Dim x As Integer

    If Len(sWindowText) = 0 Then
        FindWindow = True
        Exit Function
    End If

    iWndPtr = GetWindow(hWnd, GW_OWNER)
    If Not iWndPtr Then
        iWndPtr = hWnd
    End If

    iFound = False
    While iWndPtr And Not iFound
        sCaptionText = String$(255, 0)
        x = GetWindowText(iWndPtr, sCaptionText, 255)

        If UCase$(Left$(sCaptionText, Len(sWindowText))) =
UCase$(sWindowText) Then
            iFound = iWndPtr
        End If
        If Not iFound Then
            iWndPtr = GetWindow(iWndPtr, GW_HWNDNEXT)
        End If
    Wend
    FindWindow = iFound
End Function

I hope this helps.
Jeff Chaney

jcha...@lakers.lssu.edu