Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting the process that owns the window

Getting the process that owns the window

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
19 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tydia kun

    Hi, I've been trying to figure out how to get the owner process or a window. Basically, I want to use GetForegroundWindow() to identify the current window that has focus and then be able to, from this, get the process that owns the window. It should be possible as it's the very same thing Task Manager does. If anyone knows how to do it, I'd be grateful. Thanks.

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #3

    Found some additional info on getting the exe name from the process ID See the second Q/A here[^] Mark

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    L T 2 Replies Last reply
    0
    • M Mark Salsbery

      The GetWindowThreadProcessId() API can help. This came up recently here and I can't remember how to get the name from the process ID. I think you need to enumerate the processes on the system and find the one that matches the ID. A couple ways to enumerate processes: CreateToolhelp32Snapshot()/Process32First()/Process32Next() Use WMI and the Win32_Process class

      Tydia-kun wrote:

      It should be possible as it's the very same thing Task Manager does.

      I'm not sure why task manager would use window handles to find processes when there's many methods available for enumerating processes and task manager also shows processes that don't have any windows.  I'd guess that task manager uses WMI. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #4

      Mark Salsbery wrote:

      I'd guess that task manager uses WMI.

      Nah... probably DDE... so a couple tin cans and some string. But you can't find the documentation online because it was chiseled into stone tablets.

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        Found some additional info on getting the exe name from the process ID See the second Q/A here[^] Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #5

        If you persist in this reading of the documentation I am going to suggest that Maunder revoke your membership. or NOT FAIR! Getting answers from the documentation! It's not like we all have access to that!

        M 1 Reply Last reply
        0
        • M Mark Salsbery

          Found some additional info on getting the exe name from the process ID See the second Q/A here[^] Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          T Offline
          T Offline
          Tydia kun
          wrote on last edited by
          #6

          I've thought of getting the handle, then enumerating processes and trying to enumate windows for those processes and matching the handles, but that's seems just so far fetched. Isn't there an easier way?

          M 1 Reply Last reply
          0
          • L led mike

            Mark Salsbery wrote:

            I'd guess that task manager uses WMI.

            Nah... probably DDE... so a couple tin cans and some string. But you can't find the documentation online because it was chiseled into stone tablets.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #7

            I had to sign a NDA for Microsoft so I'm not allowed to talk about The Stone Tablets.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            1 Reply Last reply
            0
            • T Tydia kun

              I've thought of getting the handle, then enumerating processes and trying to enumate windows for those processes and matching the handles, but that's seems just so far fetched. Isn't there an easier way?

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #8

              The link I posted shows a way to do it without enumerating.  The caveat is it's for NT 4+. For Windows versions back to 95, it seems to me it would be simpler to get the process ID from the window handle and enumerate the processes until you find the matching ID. Much less work than enumerating the processes AND enumerating the windows for each process! Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              T 1 Reply Last reply
              0
              • L led mike

                If you persist in this reading of the documentation I am going to suggest that Maunder revoke your membership. or NOT FAIR! Getting answers from the documentation! It's not like we all have access to that!

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #9

                :laugh:

                led mike wrote:

                It's not like we all have access to that!

                Thank goodness!  it's job security!!

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • M Mark Salsbery

                  The link I posted shows a way to do it without enumerating.  The caveat is it's for NT 4+. For Windows versions back to 95, it seems to me it would be simpler to get the process ID from the window handle and enumerate the processes until you find the matching ID. Much less work than enumerating the processes AND enumerating the windows for each process! Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  T Offline
                  T Offline
                  Tydia kun
                  wrote on last edited by
                  #10

                  Sure, I'll use it if there's no better way, but it's pretty much the same - get the handle and compare it to the process's handles, if I read correctly. There has to be a way to get a process handle from a window handle. A process must own that window, and it's possible to enumerate a process's windows, so why not the other way?

                  M 2 Replies Last reply
                  0
                  • T Tydia kun

                    Sure, I'll use it if there's no better way, but it's pretty much the same - get the handle and compare it to the process's handles, if I read correctly. There has to be a way to get a process handle from a window handle. A process must own that window, and it's possible to enumerate a process's windows, so why not the other way?

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #11

                    Tydia-kun wrote:

                    There has to be a way to get a process handle from a window handle.

                    See the first sentence in my first reply[^] To get a handle from the ID, open one with OpenProcess(). led mike's not going to like this, but there's a whole bunch of process-related APIs to play with - they're documented ... Process and Thread Functions[^] Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    T 1 Reply Last reply
                    0
                    • T Tydia kun

                      Sure, I'll use it if there's no better way, but it's pretty much the same - get the handle and compare it to the process's handles, if I read correctly. There has to be a way to get a process handle from a window handle. A process must own that window, and it's possible to enumerate a process's windows, so why not the other way?

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      Also, your first post, to me, implied you had the window handle already. That's why I ask why enumerate every window when you have it already? Don't you just need the information for the process that created that window? Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      T 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        Tydia-kun wrote:

                        There has to be a way to get a process handle from a window handle.

                        See the first sentence in my first reply[^] To get a handle from the ID, open one with OpenProcess(). led mike's not going to like this, but there's a whole bunch of process-related APIs to play with - they're documented ... Process and Thread Functions[^] Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        T Offline
                        T Offline
                        Tydia kun
                        wrote on last edited by
                        #13

                        I have a window handle, but not a process id or process handle, because I don't know how to get either from a WINDOW handle. I know there's lots of functions and I've looked over them once or twice, but there's no function that returns a process id or handle from a window handle from what I can see.

                        M 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          Also, your first post, to me, implied you had the window handle already. That's why I ask why enumerate every window when you have it already? Don't you just need the information for the process that created that window? Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          T Offline
                          T Offline
                          Tydia kun
                          wrote on last edited by
                          #14

                          Yes, I have the hwnd. But as I mentioned, unless there's a way to get the process id or handle from a hwnd, I need to enumerate all processes and enumerate all their windows and compare those handles to the handle for the window I want to find, thus finding the owning process. Get me?

                          M 1 Reply Last reply
                          0
                          • T Tydia kun

                            I have a window handle, but not a process id or process handle, because I don't know how to get either from a WINDOW handle. I know there's lots of functions and I've looked over them once or twice, but there's no function that returns a process id or handle from a window handle from what I can see.

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #15

                            Tydia-kun wrote:

                            but there's no function that returns a process id or handle from a window handle

                            Mark wrote a couple times:

                            GetWindowThreadProcessId

                            Once again, it was in the first reply, first sentence :) Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            T 1 Reply Last reply
                            0
                            • T Tydia kun

                              Yes, I have the hwnd. But as I mentioned, unless there's a way to get the process id or handle from a hwnd, I need to enumerate all processes and enumerate all their windows and compare those handles to the handle for the window I want to find, thus finding the owning process. Get me?

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #16

                              GetWindowThreadProcessId() GetWindowThreadProcessId() GetWindowThreadProcessId()

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              1 Reply Last reply
                              0
                              • M Mark Salsbery

                                Tydia-kun wrote:

                                but there's no function that returns a process id or handle from a window handle

                                Mark wrote a couple times:

                                GetWindowThreadProcessId

                                Once again, it was in the first reply, first sentence :) Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                T Offline
                                T Offline
                                Tydia kun
                                wrote on last edited by
                                #17

                                I really must have missed it or interpreted the message wrong. Thanks again. This non-linear way of showing posts in this forum really makes a mess out of things -_-

                                M 1 Reply Last reply
                                0
                                • T Tydia kun

                                  I really must have missed it or interpreted the message wrong. Thanks again. This non-linear way of showing posts in this forum really makes a mess out of things -_-

                                  M Offline
                                  M Offline
                                  Mark Salsbery
                                  wrote on last edited by
                                  #18

                                  :) No problem!

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  L 1 Reply Last reply
                                  0
                                  • M Mark Salsbery

                                    :) No problem!

                                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                                    L Offline
                                    L Offline
                                    led mike
                                    wrote on last edited by
                                    #19

                                    Tydia-kun wrote:

                                    This non-linear way of showing posts in this forum really makes a mess out of things -_-

                                    Yeah, I guess there's a problem with the way MS does the documentation as well, I wonder why the rest of us are ok with all this.... hmmmm maybe we CAN READ! :laugh: This loser could use some Led Zeppelin[^]

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups