How to get the main window of the specified process

xiaoxiao2021-03-06  15

We know that a process may be no main window (such as a system's service process), and some process may have more than one main window (such as an Outlook software), how can we get all the main windows of the specified process? We must clarify the concept of "main window", what kind of window is the main window, he should have those features. First, the main window must be visible to have a ws_visable attribute (there are also a lot of unasible main windows, but we don't think this here) Second, the main window should be no parent window, that is, his parent window handle For null .. With these two conditions, we can do it. To get the general method of the main window of the specified process, use the EnumWindows function, enumerate all upper-layer windows that match the main window condition, and then get the process ID to which the window belongs through the getWindowThreadProcessID function, if the resulting process ID and known process The same ID is the same, indicating that the window is a main window of this process. The above is the basic idea.

The following is a design class I, to implement this function #pragma once # include #include class CProcessMainWndArray {DWORD m_dwProcessId; std :: vector m_vHwnd; public: CProcessMainWndArray (DWORD dwProcessId = 0): m_dwProcessId (dwProcessId) {m_vHwnd.clear (); EnumWindows (enumProc, (LPARAM) this);} ~ CProcessMainWndArray () {}; private: static BOOL __stdcall enumProc (HWND hWnd, LPARAM lParam) {CProcessMainWndArray * pWndIterator = (CProcessMainWndArray *) lParam; DWORD dwCurProcessId; GetWindowThreadProcessId (hWnd, & dwCurProcessId); if ((GetWindowLong (hWnd, GWL_STYLE) & WS_VISIBLE) && GetParent (hWnd) == NULL && (dwCurProcessId == pWndIterator-> getCurProcessId ())) {pWndIterator-> getMainHwndArray () -> push_back (hWnd);} return TRUE;} private: DWORD getCurProcessId () {return m_dwProcessId;} std :: vector * getMainHwndArray () {return & m_vHwnd;} public: bool isEmpty () {RETURN M_VHWND.EMPTY ();} std :: vector :: item begin () {return m_vhwnd.begin ();} std :: vector :: iterator end () {return m_vhwnd.end ( );} Size_t size () {return m_vhwnd.size ();} hwnd operator [} {return m_vhwnd [nindex];}}; this class is very simple, such as #include int Main. ) {// 0x0524 is the ID CPROCESSMAINWNDARRAY WND (0x0524) for the target process; if (wnd.isempty ()) {std :: cout << "There no main windows in target process << std :: endl; return - 1;} // First method std :: cout << "The Handles of this process area: <<

Std :: endl; std :: vector :: item @ = wnd.begin (); for (it); ore! = wnd.end (); iter) {char szhwnd [MAX_PATH] = {0} Sprintf (SZHWND, "0x% x", * iTer); std :: cout << Szhwnd << std :: end;} // second method std :: coucess is :: cout << "The Handles of this Process Are: << std :: endl; for (size_t i = 0; i getCurProcessId ())) {pWndIterator-> getMainHwndArray () -> push_back (hWnd);} Return True;} // Get Process IDDWORD GETCURPROCESSID () {return m_dwprocessid;} // Get the M_VHWnd of the record window handle Std :: Vector * getMainWndArray () {Return & m_vhwnd;} // The determination array (m_vhwnd) is empty, if it is empty, indicates that the process does not have the main window BOOL ISEMPTY () {return m_vhwnd.empty ();} // getting an iterator std :: vector

转载请注明原文地址:https://www.9cbs.com/read-48255.html

New Post(0)