How to get system process information

xiaoxiao2021-03-06  69

These days have to debug a program to view some system process information, so in order to facilitate debugging, I have written a procedure for a process. The principle is relatively simple. First get the system process ID, then listen to several modle of the ID and THREAD information. Here I am now introducing the system kernel function Toolhelp, and must include Tlhelp32.h header files in the header file before using this function. Then call the CreateToolhelp32Snapshot () function to get a handle of a system process. The module and thread information of the process are then listed through this handle. Let me introduce several important functions in the Toolhelp.

Handle CreateToolHelp32Snapshot () function

Parameters: DWORD DWFLAGS flag When the handle is returned, the information included in the handle is returned.

Here I used three logo information:

TH32CS_SNAPPROCESS: Returns the handle contains all processes information

TH32CS_SNAPMODULE: Return sentence handle contains module information for all specified processes

TH32CS_SNAPTHREAD: Returns all THREAD information with the specified process

Parameters: DWORD TH32PROCESSID The process ID number specified, if you specify as the current process

Role: Generate a snapshot

BOOL process32first () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPPROCESSENTRY32 LPPE points to the pointer to the Processentry32 structure

Role: get the first process record information from Snapshot

BOOL process32next () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPPROCESSENTRY32 LPPE points to the pointer to the Processentry32 structure

Role: Get the next process record information from Snapshot

Bool module32first () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPModuleEntry3 LPME points to a pointer to a ModuleEntry32 structure

Role: get the first module record information from Snapshot

BOOL Module32Next () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPModuleEntry3 LPME points to a pointer to a ModuleEntry32 structure

Role: get the next module record information from Snapshot

BOOL Thread32First () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPTHREADENTRY32 LPTE points to a pointer to a ThreadEntry32 structure

Role: get the first THREAD record information from Snapshot

BOOL Thread32Next () function

Parameters: Handle HSnapshot Introduced Snapshot Handle

Parameters: LPTHREADENTRY32 LPTE points to a pointer to a ThreadEntry32 structure

Role: get the next THREAD record information from Snapshot

Handle openprocess () function

Parameters: DWORD DWDESIREDACCESS Permissions Information

Here I use the process_all_access function to have all permission parameters: BOOL BINHERITHANDLE determines if the handle can be inherited

Parameters: DWPRCESSID Process ID Number

Role: Open an existing process object

The following is the test code:

Test environment: VC 7.0

Operating system: Windows2000 Professional SP4

Program function: list all the current system and the Module and Thread information of the process

///

///

// Module: ProcessTest.cpp //

// Development date: November 11, 2004 //

// Last modified date: November 11, 2004 //

// Description: CopyRight (C) 2004 Jiangjiang //

///

///

#include "stdafx.h"

#include

#include

#include

Bool getProcessList ();

Bool ListProcessModules (DWORD DWPID);

Bool ListProcessThreads (DWORD DWOWNERPID);

Void Printerror (Tchar * MSG);

INT _Tmain (int Argc, _tchar * argv [])

{

GetProcessList ();

Printf ("/ n system process is all listed! / n");

System ("pause");

Return 0;

}

Bool getProcessList ()

{

SYSTEM ("CLS");

Handle HProcessSnapshot = INVALID_HANDLE_VALUE;

Handle HProcess = NULL;

Processentry32 PE32;

DWORD dwpriorityclass;

HProcessSnapshot = CreateToolHelp32Snapshot (TH32CS_SNAPPROCESS, 0);

IF (HProcessSnapshot == Invalid_Handle_Value)

{

"" CreateToolHelp32Snapshot (of processes ");

Return (False);

}

PE32.dwsize = sizeof (ProcesSsentry32);

IF (! process32first (hprocesssnapshot, & pe32))

{

Printerror ("Process32First");

CloseHandle (HProcessSnapshot); // Note! Don't forget to release the handle when you don't use it! ! !

Return (false);

DO

{

Printf ("/ n / n =========================================== =========== ");

Printf ("/ n process name:% s", pe32.szexefile;

Printf ("/ n ------------------------------------------- -------- ");

DWPRIORITYCLASS = 0;

HProcess = openprocess (process_all_access, false, pe32.th32processid);

IF (hprocess == null)

{

Printerror ("Open Process Error!");

}

Else

{

DWPRIORITYCLASS = GetPriorityClass (HProcess);

IF (! dwpriorityClass)

PrintError ("Get priority levels!");

CloseHandle (HPROCESS); // Note! Don't forget to release the handle when you don't use it! ! !

}

Printf ("/ n process ID: 0x% 08X", pe32.th32processid;

Printf ("/ N thread count:% D", pe32.cntthreads;

Printf ("/ n parent process ID: 0x% 08X", pe32.th32parentProcessID);

Printf ("/ N Basic Priority:% D", PE32.PCPriclassBase);

IF (dwpriorityClass)

{

Printf ("/ N Process Priority:% D", DWPRIORITYCLASS;

}

ListProcessModules; PE32.TH32PROCESSID;

ListProcessthreads (pe32.th32processid);

Printf ("/ n");

System ("pause");

}

While (Process32Next (HProcessSnapshot, & PE32));

CloseHandle (HProcessSnapshot); // Note! Don't forget to release the handle when you don't use it! ! !

Return (TRUE);

}

Bool ListProcessModules (DWORD DWPID)

{

Handle hModulesnapshot = invalid_handle_value;

ModuleEntry32 me32;

ME32.dwsize = sizeof (moduleEntry32);

HModulesnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, DWPID);

IF (hmodulesnapshot == invalid_handle_value)

{

"" CreateToolhelp32Snapshot (of modules) "; Return (false);

}

IF (! module32first (hmodulesnapshot, & me32))

{

Printerror ("Module32First Error!");

CloseHandle (hmodulesnapshot); // Note! Don't forget to release the handle when you don't use it! ! !

Return (False);

}

DO

{

Printf ("/ n / n module name:% s", me32.szmodule);

Printf ("/ N Run Path:% S", Me32.Szexepath);

Printf ("/ n process ID: 0x% 08X", me32.th32processid;

Printf ("/ N global count value: 0x% 04x", me32.glblcntusage);

Printf ("/ n process count value: 0x% 04x", me32.proccntusage);

Printf ("/ N base address: 0x% 08X", (dword) me32.modbaseaddr);

Printf ("/ N base site size:% D", me32.modbasesize;

}

While (Module32Next (HModulesnapshot, & me32));

CloseHandle (hmodulesnapshot); // Note! Don't forget to release the handle when you don't use it! ! !

Return (TRUE);

}

Bool ListProcessThreads (DWORD DWOWNERPID)

{

Handle HProcessthread = Invalid_Handle_Value;

ThreadENTRY32 TE32;

TE32.dwsize = sizeof (threadentry32);

HProcessThread = CreateToolHelp32Snapshot (TH32CS_SNAPTHREAD, DWOWNERPID);

IF (hprocessthread == invalid_handle_value)

{

Return (False);

}

IF (! thread32first (hprocessthread, & te32))

{

Printerror ("Thread32First");

CloseHandle (HProcessThread);

Return (False);

}

DO

{

IF (te32.th32ownerprocessid == dwownerpid)

{

Printf ("/ n / n thread ID: 0x% 08X", te32.th32threadid);

Printf ("/ N-based priority:% D", te32.tpbasepri;

}

}

While (thread32next (hprocessthread, & te32));

CloseHandle (HProcessThread);

Return (TRUE);

}

Void Printerror (tchar * msg)

{

DWORD ENUM;

Tchar sysmsg [256];

Tchar * p;

ENUM = getLastError ();

FormatMessage (format_message_from_system | format_message_ignore_inserts, null, enum,

Makelangid (Lang_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),

SYSMSG, 256, NULL;

P = sysmsg;

While (* p> 31) || (* p == 9))))))

P;

Do {* p - = 0;} while ((p> = sysmsg) &&&

((* p == '.') || (* P <33))));

Printf ("/ N Warning:% s Failed with Error% D (% s)", MSG, Enum, SYSMSG;

}

转载请注明原文地址:https://www.9cbs.com/read-87315.html

New Post(0)