PlanetSquires Forums

Support Forums => WinFBE - Code Editor and Visual Designer => Topic started by: jermy on May 20, 2021, 02:31:22 PM

Title: Deadlock if window not found
Post by: jermy on May 20, 2021, 02:31:22 PM
How come I get a deadlock if the window is not found?
if the window is found, nothing is wrong

Code: [Select]
' ========================================================================================
' 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
' ========================================================================================
Title: Re: Deadlock if window not found
Post by: Paul Squires on May 23, 2021, 06:18:18 AM
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.
Title: Re: Deadlock if window not found
Post by: jermy on May 25, 2021, 07:13:37 PM
somehow it doesn't work here.
does not matter, we do it differently

Code: [Select]
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
Title: Re: Deadlock if window not found
Post by: Josť Roca on May 26, 2021, 01:10:46 AM
The return parameter must be LONG, not BOOL, and you must return CTRUE (1) not TRUE (-1).
Title: Re: Deadlock if window not found
Post by: Paul Squires on May 26, 2021, 07:11:20 AM
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?
Title: Re: Deadlock if window not found
Post by: 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.
Title: Re: Deadlock if window not found
Post by: Wilko Verweij on May 27, 2021, 05:15:52 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 (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