Windows CE provides a subset of the "toolhelp" functions that provide information on, among other things:
These functions work by first creating a snapshot of the required information (for example, the list of processes). Calling the function CreateToolhelp32Snapshot does this. Then, the appropriate enumeration functions are called to obtain information about each object in turn. For processes, the functions are Process32First and Process32Next. Listing 5.4 shows code for listing all processes running on a device, together with the number of threads and process identifier. You need to include the file "tlhelp32.h" and add toolhelp.lib when using these functions. Listing 5.4 Lists running processes#include <Tlhelp32.h> // Add toolhelp.lib to the project void Listing5_4() { HANDLE hProcessSnap; PROCESSENTRY32 pe32; // Take a snapshot of all processes currently running. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == (HANDLE)-1) { cout _T("Could not take Toolhelp snapshot") endl; return ; } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { do { cout pe32.szExeFile _T(" Threads: ") pe32.cntThreads _T(" ProcID: ") pe32.th32ProcessID endl; } while (Process32Next(hProcessSnap, &pe32)); } CloseToolhelp32Snapshot(hProcessSnap); return ; } The function CreateToolhelp32Snapshot is passed a constant for the first parameter that defines the information to be included in the snapshot. Inthis case TH32CS_SNAPPROCESS specifies that a snapshot of processes be produced. This function can also be used to create snapshots of the heap list (TH32CS_SNAPHEAPLIST), the modules being used by a process (TH32CS_SNAPMODULE), and the threads for a process (TH32CS_SNAPTHREAD). These constants can be combined to create a snapshot that contains several different objects, or TH32CS_SNAPALL can be used to specify that all objects should be included. The second argument in CreateToolhelp32Snapshot specifies the process identifier of the process to be included in the snapshot in this case "0" indicates all processes. A handle is returned from CreateToolhelp32Snapshot that is used when enumerating the objects. The function Process32First returns information about the first process in the snapshot. The function is passed the handle returned from CreateToolhelp32Snapshot, and a pointer to a PROCESSENTRY32 structure into which the information is placed. Note that the dwSize member of PROCESSENTRY32 must first be initialized with the size of the structure. The function Process32Next is called to obtain information about the next process A return of TRUE indicates that another process's information was copied into the structure, and FALSE indicates the enumeration is complete.
Table 5.4 shows the PROCESSENTRY32 members used in Windows CE and their purpose.
|