Traverse all windows owned by a process

Source: Internet
Author: User
# Include <windows. h> # include <tlhelp32.h> # include <atlstr. h> # include <locale. h> # define window_text_length 256 bool callback enumchildwindowcallback (hwnd, lparam) {DWORD dwpid = 0; getwindowthreadprocessid (hwnd, & dwpid ); // obtain the process to which the window belongs if (dwpid = lparam) // determine whether the window is the target process {printf ("0x % 08x", hwnd ); // output window information tchar Buf [window_text_length]; sendmessage (hwnd, wm_gettext, window_text_len Callback, (lparam) BUF); wprintf (L "% s/n", Buf); enumchildwindows (hwnd, enumchildwindowcallback, lparam ); // recursive search subwindow} return true;} bool callback enumwindowcallback (hwnd, lparam) {DWORD dwpid = 0; getwindowthreadprocessid (hwnd, & dwpid ); // obtain the process to which the window belongs if (dwpid = lparam) // determine whether the window is the target process {printf ("0x % 08x", hwnd ); // output window information tchar Buf [window_text_length]; sendmessage (hwnd, wm_gettext, window _ Text_length, (lparam) BUF); wprintf (L "% s/n", Buf); enumchildwindows (hwnd, enumchildwindowcallback, lparam ); // continue searching subwindow} return true;} int main () {setlocale (lc_ctype, "CHS"); DWORD qqpid = 0; // process ID processentry32 PE; // Process Information PE. dwsize = sizeof (processentry32); handle hsnapshot = createconlhelp32snapshot (th32cs_snapprocess, 0); // process snapshot if (! Process32first (hsnapshot, & PE) // obtain the snapshot of the first process. Return 0; do {If (! Process32next (hsnapshot, & PE) return 0;} while (strcmp (PE. szexefile, l "qq.exe"); // traverses the process until you find the target process qqpid = PE. th32processid; wprintf (L "find qq.exe process: 0x % 08x/N", qqpid); enumwindows (enumwindowcallback, qqpid); Return 0 ;}

1. two methods for obtaining window content: getwindowtest (hwnd, szout); sendmessage (hwnd, wm_gettext, nlen, szout); the two methods are the same for In-process operations. However, during cross-process operations, the former only returns the title of the window (some windows may not have the title), and the latter returns the text of the window. 2. If wprintf needs to output Chinese characters, add setlocale (lc_ctype, "CHS") in the program portal; 3. enumwindows can only enumerate top-level windows, and you need to call enumchildwindows recursively to obtain all windows of the process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.