PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Deadlock if window not found  (Read 400 times)

jermy

  • Junior Member
  • **
  • Posts: 98
Deadlock if window not found
« 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
' ========================================================================================
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9391
  • Windows 10
    • PlanetSquires Software
Re: Deadlock if window not found
« Reply #1 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.
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

  • Junior Member
  • **
  • Posts: 98
Re: Deadlock if window not found
« Reply #2 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
Logged

Josť Roca

  • Guru Member
  • *****
  • Posts: 3327
Re: Deadlock if window not found
« Reply #3 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).

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9391
  • Windows 10
    • PlanetSquires Software
Re: Deadlock if window not found
« Reply #4 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?
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

  • Junior Member
  • **
  • Posts: 98
Re: Deadlock if window not found
« Reply #5 on: May 26, 2021, 02:33:32 PM »

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

Wilko Verweij

  • Junior Member
  • **
  • Posts: 168
  • PB10 - FF3.7 User; Win 10
    • CHEAQS
Re: Deadlock if window not found
« Reply #6 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
I have implemented this in FreeBasic somewhere. If you need it I can dig it up for you.
Wilko
Logged