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.
  • 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