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. How to get A WindowHandle from a process Id

How to get A WindowHandle from a process Id

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
9 Posts 6 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.
  • R Offline
    R Offline
    Rajmohan SK
    wrote on last edited by
    #1

    Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

    Regards Rajmohan

    C U C C M 7 Replies Last reply
    0
    • R Rajmohan SK

      Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

      Regards Rajmohan

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Which window handle? There are usually more windows belonging to a process (e.g. controls on a dialog like buttons, edit fields have a window handle too). Anyways, i assume you are looking for a top level popup window, one method would be to enumerate all the windows, for example using EnumWindows[^], on every window you could call GetWindowThreadProcessId[^] to get the id of the process the window belongs to and test for equality...

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      1 Reply Last reply
      0
      • R Rajmohan SK

        Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

        Regards Rajmohan

        U Offline
        U Offline
        User 3919723
        wrote on last edited by
        #3

        The handle of what window? To retrieve all windows of a process, you have to call EnumWindows and, for each window, use GetWindowThreadProcessId to check if a window belongs to a process (the ProcessID of the window equals the ProcessID of the process).

        R 1 Reply Last reply
        0
        • R Rajmohan SK

          Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

          Regards Rajmohan

          C Offline
          C Offline
          Cvaji
          wrote on last edited by
          #4

          GetWindowThreadProcessId

          what about this?

          1 Reply Last reply
          0
          • R Rajmohan SK

            Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

            Regards Rajmohan

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            See here [^]. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            1 Reply Last reply
            0
            • R Rajmohan SK

              Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

              Regards Rajmohan

              M Offline
              M Offline
              Michel Godfroid
              wrote on last edited by
              #6

              There is no single window handle. A process may have multiple main windows, or none at all. So you have to enumerate all windows, and select the ones which have no owner, and the correct process id. Those will be 'main' windows of the process. And before thou protests: Yes, I know that in .Net there is a property called System.Diagnostics.Process.MainWindowHandle, but the implementation is just plain wrong.

              1 Reply Last reply
              0
              • R Rajmohan SK

                Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

                Regards Rajmohan

                R Offline
                R Offline
                Rajmohan SK
                wrote on last edited by
                #7

                Thank you so much for all the support. I forget to mention the Window Handle,It is of-course the parent window itself

                Regards Rajmohan

                1 Reply Last reply
                0
                • U User 3919723

                  The handle of what window? To retrieve all windows of a process, you have to call EnumWindows and, for each window, use GetWindowThreadProcessId to check if a window belongs to a process (the ProcessID of the window equals the ProcessID of the process).

                  R Offline
                  R Offline
                  Rajmohan SK
                  wrote on last edited by
                  #8

                  EnumWindows function is working fine with all windows except my application. I will describe the issue in detail. I have a dll application and I have loaded it into App_Init folder, which loads for every application. I want to know which is currently loaded application and also I need to get the handle of the main window. I used EnumWindows for navigating through opened application. It is calling for all the application except mine. I dont understand the issue can anybody help me. Thanks in advance

                  Regards Rajmohan

                  1 Reply Last reply
                  0
                  • R Rajmohan SK

                    Can anybody tell how I get the window handle from a specific process Id. I got the process Id using GetCurrentProcessId()

                    Regards Rajmohan

                    R Offline
                    R Offline
                    Rajmohan SK
                    wrote on last edited by
                    #9

                    EnumWindows function is working fine with all windows except my application. I will describe the issue in detail. I have a dll application and I have loaded it into App_Init folder, which loads for every application. I want to know which is currently loaded application and also I need to get the handle of the main window. I used EnumWindows for navigating through opened application. It is calling for all the application except mine. I dont understand the issue can anybody help me. Thanks in advance

                    Regards Rajmohan

                    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