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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Window Handle and Process Handle

Window Handle and Process Handle

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 1 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.
  • S Offline
    S Offline
    Subramaniam s V
    wrote on last edited by
    #1

    Hi, What is the difference between a process handle and a window handle? Is there any convertions between the two? Thanks

    M S 2 Replies Last reply
    0
    • S Subramaniam s V

      Hi, What is the difference between a process handle and a window handle? Is there any convertions between the two? Thanks

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      A window handle (a HWND) identifies a window and a process handle (HANDLE) enables you to monitor/control a process. There is no direct meaningful conversion between them. That said writing code what enumerates all the windows handles in a given process is possible. It is also possible to go from a HWND to a HANDLE for the owning process as follows:

      DWORD dwProcessID;
      GetWindowThreadProcessId(hWnd, &dwProcessID);
      HANDLE hProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessID);
      

      Steve

      1 Reply Last reply
      0
      • S Subramaniam s V

        Hi, What is the difference between a process handle and a window handle? Is there any convertions between the two? Thanks

        M Offline
        M Offline
        Monty2
        wrote on last edited by
        #3

        HWND is a Handle to a Window Where as process handle is a handle to a process (Note: a process may or maynot have a window) So Both are different things, the correct question will be how can i get the process id if i have the HWND of one of its windows GetWindowThreadProcessId gives the process id to get the opposite (processid to window handle) you have to enumerate all the windows and compare the process id.


        C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

        S 1 Reply Last reply
        0
        • M Monty2

          HWND is a Handle to a Window Where as process handle is a handle to a process (Note: a process may or maynot have a window) So Both are different things, the correct question will be how can i get the process id if i have the HWND of one of its windows GetWindowThreadProcessId gives the process id to get the opposite (processid to window handle) you have to enumerate all the windows and compare the process id.


          C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

          S Offline
          S Offline
          Subramaniam s V
          wrote on last edited by
          #4

          Yeah. I do accept that a window handle is a handle to a window and a process handle is a handle to a process and not all the time a process can have a window. But, Internet Explorer has a window by default. If I am gonna launch IE from my application as a process I get the process handle for IE. At this point of time, will I be able to get the window handle of the new IE process launched or is there any methods that converts my process ID got(from createprocess functions) to a window handle.

          M S 2 Replies Last reply
          0
          • S Subramaniam s V

            Yeah. I do accept that a window handle is a handle to a window and a process handle is a handle to a process and not all the time a process can have a window. But, Internet Explorer has a window by default. If I am gonna launch IE from my application as a process I get the process handle for IE. At this point of time, will I be able to get the window handle of the new IE process launched or is there any methods that converts my process ID got(from createprocess functions) to a window handle.

            M Offline
            M Offline
            Monty2
            wrote on last edited by
            #5

            any methods that converts my process ID got(from createprocess functions) to a window handle. First of all don't use *convert*, second open Spy++ and open up IE then you will see that IE has lots windows, not just one window (toolbars,menus ...) so the question is which window's handle you want what do you want to do anyway ?


            C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

            1 Reply Last reply
            0
            • S Subramaniam s V

              Yeah. I do accept that a window handle is a handle to a window and a process handle is a handle to a process and not all the time a process can have a window. But, Internet Explorer has a window by default. If I am gonna launch IE from my application as a process I get the process handle for IE. At this point of time, will I be able to get the window handle of the new IE process launched or is there any methods that converts my process ID got(from createprocess functions) to a window handle.

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              As Monty v2.0 said, a mapping from a process to a HWND is a one to many mapping. Here's how you would go about doing it however:

              // Add this function.
              BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
              {
                   DWORD dwProcessID;
                   GetWindowThreadProcessId(hwnd, &dwProcessID)
                   if ( dwProcessID == lParam )
                   {
                       // "hwnd" is a top level window belonging to the process.
                   {
               
                   return TRUE;
              }
               
              // Add this code directly after calling CreateProcess. pi is the PROCESS_INFORMATION struct.
              CloseHandle(pi.hThread);
              WaitForInputIdle(pi.hProcess, INFINITE); // Wait for for IE...
              CloseHandle(pi.hProcess);
              EnumWindows(&EnumWindowsProc, pi.dwProcessId);
              

              I have not actually tested this code but the idea is sound. Now that I've given this example here's some advice - DON'T USE IT (unless you have no choice)! You should look into controlling IE through OLE Automation. Steve

              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