ArticleDirectory
- 1. Reference The tlhelp32.h File
- 2. polling process information
- 3. Display Process Information
- 4. Link toolhelp. Lib
When using native C ++ for development, he asked me how to retrieve the handle of the window in the process. Because process information is required to retrieve the handle in the process, I will first summarize how to retrieve information about all running processes.
1. Reference The tlhelp32.h File
# Include"Tlhelp32.h"
Createconlhelp32snapshot, process32first, and process32next are used to query process information.
2. polling process information
Void Getrunningprocesses ()
{
Processes. Clear ();
Handle hsnapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
// Do a little error handling, just in case.
If (Hsnapshot = (handle)-1)
{
Wprintf (text ( "Getrunningprocesses: Failed createconlhelp32snapshot error: % d \ n" ),
Getlasterror ());
Return ;
}
Processentry32 process;
Process. dwsize = Sizeof (Processentry32 );
Bool retval = process32first (hsnapshot, & process );
While (Retval)
{
Processes. push_back (process );
Retval = process32next (hsnapshot, & process );
}
// Always good to close those handles. then return the number of processes that we found.
Closetoolhelp32snapshot (hsnapshot );
}
This is the focus of this article. I put the queried information into a global list, which may be encapsulated as an object.
# Include<List>
STD: List <processentry32> processes;
3. Display Process Information
Void Showrunningprocesses ()
{
Getrunningprocesses ();
DWORD maxprocessnamelength = getmaxprocessnamelength ();
// Output a header to describe each column
Wprintf (text ( "%-* S % 8 S % 13 S % 9 S % 9 S % 10s \ n" ),
Maxprocessnamelength,
Text ( "Process" ),
Text ( "PID" ),
Text ( "Base priority" ),
Text ( "# Threads" ),
Text ( "Base ADDR" ),
Text ( "Access key" )
);
// 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
);
}
}
DWORD getmaxprocessnamelength ()
{
DWORD maxlength = 0;
DWORD currentlength;
For (STD: List <processentry32 >:: iterator it = processes. Begin ();
It! = Processes. End (); ++ it)
{
Currentlength = wcslen (IT-> szexefile );
If (Maxlength <currentlength)
{
Maxlength = currentlength;
}
}
Return Maxlength;
}
Print process information to the console. Because Windows Mobile does not have a console, the display methods are different, but they are all read and displayed from the List (processes.
4. Link toolhelp. Lib
Finished. The running effect is as follows:
The aboveCodeReference http://geekswithblogs.net/BruceEitman/archive/2008/05/14/windows-ce--using-toolhelpapi-to-list-running-processes.aspx
I have also written an article on how to manage processes under. NET Compact framework. For more information, see
How to use the. NET Compact framework development process management program in Windows Mobile and Windows WinCE (Windows Embedded CE)
The next article describes how to perform Win32 development and retrieve the window handle in Windows Mobile and Windows Embedded ce.