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. using process to look for hwnd

using process to look for hwnd

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

    i use CreateToolhelp32Snapshot function to seach all the process id , but i can not use process id to look for the main frame hwnd!! can anyone tell me how to find that Thanks hi

    S 1 Reply Last reply
    0
    • Z zt9788

      i use CreateToolhelp32Snapshot function to seach all the process id , but i can not use process id to look for the main frame hwnd!! can anyone tell me how to find that Thanks hi

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

      Use EnumWindows to enumerate all top level windows. For each window enumerated use the GetWindowThreadProcessId function to filter the windows to those belonging to the desired process. Steve

      Z 1 Reply Last reply
      0
      • S Stephen Hewitt

        Use EnumWindows to enumerate all top level windows. For each window enumerated use the GetWindowThreadProcessId function to filter the windows to those belonging to the desired process. Steve

        Z Offline
        Z Offline
        zt9788
        wrote on last edited by
        #3

        There are a lot of hWnds in a top level window, such as Static, Button, EditBox etc. Using EnumWindow to look for hWnd wastes a lot of resources of system, and makes the system slowly. Can anybody give me a good idea!! thanks. hi

        S 1 Reply Last reply
        0
        • Z zt9788

          There are a lot of hWnds in a top level window, such as Static, Button, EditBox etc. Using EnumWindow to look for hWnd wastes a lot of resources of system, and makes the system slowly. Can anybody give me a good idea!! thanks. hi

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

          EnumWindows only enumerates top level windows. The procedure I outlined doesn't waste system resources - It's how it's done! Steve

          Z 1 Reply Last reply
          0
          • S Stephen Hewitt

            EnumWindows only enumerates top level windows. The procedure I outlined doesn't waste system resources - It's how it's done! Steve

            Z Offline
            Z Offline
            zt9788
            wrote on last edited by
            #5

            Thanks hi

            S 1 Reply Last reply
            0
            • Z zt9788

              Thanks hi

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

              Here's an example that uses the technique: ------------------------------------------ // Win32App.cpp : Defines the entry point for the application. // #include "stdafx.h" #include #include #include #include typedef std::vector HWNDS; namespace { struct EnumInfo { DWORD dwProcessID; HWNDS *pHWNDS; }; BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam ) { const EnumInfo *pInfo = reinterpret_cast(lParam); DWORD dwProcessID; GetWindowThreadProcessId(hWnd, &dwProcessID); if ( dwProcessID == pInfo->dwProcessID ) { pInfo->pHWNDS->push_back(hWnd); } return TRUE; } } BOOL GetProcessTopLevelWindows( DWORD dwProcessId, HWNDS *pOut ) { assert(pOut); EnumInfo ei = {dwProcessId, pOut}; return EnumWindows(&EnumWindowsProc, reinterpret_cast(&ei)); } struct ShowWindowTitle { bool operator()(HWND hWnd) const { int Len = GetWindowTextLength(hWnd); if ( Len>0 ) { char *pText = reinterpret_cast(_alloca(Len+1)); GetWindowText(hWnd, pText, Len+1); MessageBox(NULL, pText, "Window title", MB_OK); return true; } return false; } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWNDS wins; if ( GetProcessTopLevelWindows(2216, &wins) ) // PID HARDCODED! { std::for_each(wins.begin(), wins.end(), ShowWindowTitle()); } return 0; } 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