Use Windows Mobile and Windows Embedded ce for Win32 development and retrieve the window handle.

Source: Internet
Author: User

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.

Related Article

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.