In the previous articleArticleThe method for retrieving process information is described as follows:
Perform Win32 development in Windows Mobile and Windows WinCE (Windows Embedded CE) to retrieve information about all running processes.
This article describes how to retrieve the corresponding window handle of a process.
ProgramIt was modified in the example of the previous article.
CoreCodeAs follows:
Processwndsinfo procinfo;
Procinfo. processid = it-> th32processid;
If(Enumwindows (enumwindowsproc, (lparam) & procinfo) = false)
{
Wprintf (text ("Enumwindowsproc failed. Error: % d \ n"),
Getlasterror ());
Continue;
}
Call enumwindows () to retrieve information about all windows in a process. The first parameter of the enumwindows () function is the pointer of the callback function. The Code is as follows:
Bool callback enumwindowsproc (hwnd, lparam)
{
Processwndsinfo * procinfo = (processwndsinfo *) lparam;
DWORD processid;
Getwindowthreadprocessid (hwnd, & processid );
If(Procinfo! = NULL & processid = procinfo-> processid)
{
Procinfo-> windowshandles. push_back (hwnd );
}
// Keep enumerating
Return true;
}
Because the enumwindows () function polls all windows, each window calls back enumwindowsproc () once. Therefore, you need to determine the process ID of the window. If it belongs to this process, it should be included in the list.
Processwndsinfo is defined as follows:
StructProcesswndsinfo
{
DWORD processid;
STD: List };
Contains the process ID and the list of handles of all windows in the process.
The following code displays the window handle information.
// Output information for each running process
For (STD: List <processentry32 >:: iterator it = processes. Begin ();
It! = Processes. End (); ++ it)
{
Wprintf (text ( "%-* S % 8x % 13D % 9d % 9x % 10x \ n" ),
Maxprocessnamelength,
It-> szexefile,
It-> th32processid,
It-> pcpriclassbase,
It-> cntthreads,
It-> th32memorybase,
It-> th32accesskey
);
Processwndsinfo procinfo;
Procinfo. processid = it-> th32processid;
If (Enumwindows (enumwindowsproc, (lparam) & procinfo) = false)
{
Wprintf (text ( "Enumwindowsproc failed. Error: % d \ n" ),
Getlasterror ());
Continue ;
}
// Output a header to describe the hwnd
Wprintf (text ( "\ Thwnd \ t \ twindows title \ n" ));
Wchar title [255];
For (STD: List Ithwnd! = Procinfo. windowshandles. End (); ++ ithwnd)
{
// Get title of the window
Getwindowtext (* ithwnd, title, 255 );
Wprintf (text ( "\ T % 10x \ t % s \ n" ), * Ithwnd, title );
}
}
To display more detailed information, I will display the window names together. The effect is as follows:
Source code: Http://files.cnblogs.com/procoder/GetHWnd-without-project-file.rar
Because I developed it under wince, it is not a general platform. I deleted the project file and uploaded the source code for reference only.