# 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.