No announcement yet.

Window handle

  • Filter
  • Time
  • Show
Clear All
new posts

  • Window handle

    If I have a process handle, how do I get the handle to it's window? I now use findwindow, but that can deliver a false handle if multiple instances of a program are active.

    "Simplicity is a prerequisite for reliability"

  • #2
    Peter --
    You didn't described exactly, why do you need to know hWnd.
    It's possible to imagine, that you start any program by Shell and want to operate with a window of started program.
    If you can operate with caption of shelled program, you can create unique id., using, for example, Timer and then you will be able to find correct window by FindWindow.

    If you can't operate with caption, I can offer one stupid solution.
    Before Shell you receive a list of windows, with interesting for you captions.
      Dim Title As Asciiz * 256
      hWnd& = GetDesktopWindow
      hWnd& = GetWindow(hWnd&, %GW_CHILD)
         If hWnd& = 0 Then Exit Do
         Rc& = GetWindowText(hWnd&, Title$, 255)
         If Title$ = "..." Then ...
         hWnd& = GetWindow(hWnd&, %GW_HWNDNEXT)
    After Shell (and small delay) you can receive a new list and compare with previous.

    It's not a good solution and I offered something, because there are no other messages only (but inside I hope that somebody offers something better).


    • #3
      If you have more than one window with the same title, you can tell the difference using GetWindowLong() and looking at GWL_HINSTANCE to see who owns the window.


      PowerBASIC Support
      mailto:[email protected][email protected]</A>

      Home of the BASIC Gurus


      • #4
        Dave --
        It's possible under Win16, but not under Win32, where each application has its own address space.
        Like I understand, it's necessary to use process id., but I have no idea how to receive it, if hWnd is known.

        [This message has been edited by Semen Matusovski (edited January 17, 2000).]


        • #5
          Peter --

          > If I have a process handle, how do
          > I get the handle to it's window?

          You have to keep in mind that each Windows process is allowed to have more than one top-level window so, technically, your question can have more than one answer, depending on the application that you're talking about. Right now on my system, according to Microsoft Spy++, Netscape has over a dozen top-level windows open. Since the API is (generally) designed to take all possible circumstances into account, the most you could probably hope for would be an "enumerate" API function that would list all of the windows that are associated with a process. For example, EnumThreadWindows can be used to enumerate all of the windows that are associated with a thread. If you can get the thread handle for the target process you might be able to use that API, and then sort out the parent-child relationships with the GetParent API (etc.). But remember that each process can have multiple threads, too.

          Or you could use the generic EnumWindows API to enumerate all of the system's current top-level windows, and then use the GetWindowThreadProcessId to find out if the process ID that you are looking for produced the window(s). I recently posted an EnumWindows function in the Source Code forum. It could easily be modifed to perform a GetWindowThreadProcessId check.

          -- Eric

          Perfect Sync: Perfect Sync Development Tools
          Email: mailto:[email protected][email protected]</A>

          [This message has been edited by Eric Pearson (edited January 17, 2000).]
          "Not my circus, not my monkeys."