• Welcome to PlanetSquires Forums.
 

Deadlock if window not found

Started by jermy, May 20, 2021, 02:31:22 PM

Previous topic - Next topic

jermy

How come I get a deadlock if the window is not found?
if the window is found, nothing is wrong


' ========================================================================================
' Retrieves the window handle given it's window name
' ========================================================================================
PRIVATE FUNCTION GethWndFromName (BYVAL sName AS cwstr) AS HWND
   DIM nLen AS LONG = Len(sName)
    DIM cwsText AS CWSTR = SPACE(nLen + 1)

   ' // Get the first window handle
   DIM hwnd AS HWND = FindWindowW(NULL, NULL)

   ' // Enumerate all the windows
   WHILE hwnd <> NULL
      ' // If the parent window is NULL, it's a top level window
      IF GetParent(hwnd) = NULL THEN
     
         SendMessageW(hwnd, WM_GETTEXT, nLen + 1, cast(LPARAM, *cwsText))
         if sName = cwsText then  ' // We have found it ?
            FUNCTION = hwnd
            EXIT WHILE
         END IF
       
       END IF
      ' // Get the next window handle
       hwnd = GetWindow(hwnd, GW_HWNDNEXT)     
   WEND
END FUNCTION
' ========================================================================================

Paul Squires

I had no trouble calling your function to search for a visible window. It never seemed to cause a infinite loop.

Having said that, here is what EnumWindows in the api has to say:

Quote
This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

somehow it doesn't work here.
does not matter, we do it differently


declare Function EnumHwndProc(Byval hwnd As HWND, Byval lParam As LPARAM) As BOOL

                      EnumChildWindows( hWnd, @EnumHwndProc, 0)   

Function EnumHwndProc(Byval hwnd As HWND, Byval lParam As LPARAM) As BOOL

? hwnd

    Return TRUE '' Continue enumeration.
End Function

José Roca

The return parameter must be LONG, not BOOL, and you must return CTRUE (1) not TRUE (-1).

Paul Squires

Can you explain what it is you are trying to do? Are you searching for top level windows or top level windows and all of their respective child windows? Are you just trying to see if another application is open and/or active?
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

i have it working now, i was trying to find out if a program is running.

Wilko Verweij

Quote from: jermy on May 26, 2021, 02:33:32 PM
i have it working now, i was trying to find out if a program is running.
I think the recommended way to do this is to take a snapshot: https://docs.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes
I have implemented this in FreeBasic somewhere. If you need it I can dig it up for you.
Wilko