Enumeration process

zhaozj2021-02-16  47

Enumeration process: a method of practice: Irfan Dawood Source: http: //www.codeproject.com/threads/processes.asp Translator: zhf0021

Introduction First, welcome to "Enumeral Processes: A Practical Method" this journey. Here, I will try to define what is process, then we will use C and Toolhelp32 API to find all the processes that are running on our machine.

What is a process process is an instance of running a program. In the system, it is possible that a program has multiple runs, and they are running separately. A process can generate a child process, and the process of generating a child is also called a parent process. This is not the same in the object-oriented inheritance. In the object, we can generate an instance of subclasses without geneting instances of parental class. To generate a child process must first generate a parent process, while the child process can use the resources of the parent process.

Explain that I am using Visual C 6.0, the engineering type is Win32 console application (for simple), we will use the Toolhelp32 API. I am using Win2000, I hope to run on 9x. For NT, we use the PSAPI (Process Status API) function, here we will not discuss them.

The first thing to include the necessary header files: #include #include #include #include

Using namespace std;

INT main () {cout << Endl << "Running Processes" << ENDL; now we will use the function createtoolhelp32snapshot () to get a snapshot of the current running process, this function returns to the included running

The snapshot handle of the process. His original shape is: Handle WinApi CreateToolHelp32Snapshot (DWORD DWFLAGS, DWORD TH32PROCESSID); we set DWFLAGS to TH32CS_SNAPPROCESS, TH32ProcessID set to 0. See MSDN for other options.

Handle hsnapshot = CreateToolHelp32Snapshot (TH32CS_SNAPPROCESS, 0);

Now we have information about all processes. We will extract data from hsnapshot into a processentry32 structure, this structure

Represents a process and is part of the Toolhelp32 API. The data is extracted by Process32First () and Process32Next ().

Here we use process32next (), his original shape is: BOOL WINAPI Process32Next (Handle Hsnapshot, LPPROCESSENTRY32 LPPE); Join the code of our program:

Processentry32 * processinfo = new processentry32;

The value of the DWSIZE member of Processentry32 must be set;

ProcessInfo-> dwsize = sizeof (processentry32); int index = 0;

Here we will pass the snubbed handle and the processentry32 to process32next (). After execution, the Processentry32 structure will obtain the information of the process. We circulate traversal until the function returns false.

While (process32next (hsnapshot, processinfo)! = false) {cout << Endl << "*************************************** ***************** "; cout << Endl <<" / t / t / t "<< index; cout << Endl << **** ******************************************* "; COUT << Endl < <"Parent Process ID: << ProcessInfo-> Th32ParentProcessId; coucess ID: << Endl <<" Process ID: << ProcessInfo-> Th32ProcessID; coucess << endl << "Name: << processinfo-> szexefile; cout << Endl << "CURRENT Threads: << Processinfo-> CNTTHREADS; cout << Endl <<" CURRENT USAGE: << ProcessInfo-> cntusage; cout << Endl << "Flags: << processinfo-> DWFlags; coup << Endl << "Size:" "processinfo-> dwsize; cout << Endl <<" primary class base: << processinfo-> pcpriclassbase; cout << Endl << "default Heap ID:" << processinfo-> th32defaultheapid; coudui ID: << Endl << "Module ID: << Processinfo-> TH32ModuleId;

Don't forget to close your handle:

CloseHandle (HSNAPSHOT); cout << endl; cout << Endl << "******************************************** *************** "; cout << Endl << endl;

Now we have all information that is running the process, including process ID, file name, parent process ID, etc. We can

Use the function openprocess () to get the process ID. Handle OpenProcess (DWord DwdesiredAccess, // Access Flag Bool BinheritHandle, // Handle Inheritance Option DWord DWProcessid // Process Identifier); See MSDN in detail. INT processid; cout << "Enter processid to get handle of the process:"; cin >> processid;

Here we use process_all_access handle hndle hnd = openprocess (processid); if (hprocess == null) {cout << "unable to get handle of process: << processid; cout <<" error is: "<<" ERROR IS: << GetLastError (); return 1;} Now we have a handle of the process, you can do magic things! Let's get the process of getpriorityclass () to get the process.

Priority, then set priority with setPriorityClass ():

Cout << Endl << "Priority Class: << GetPriorityClass (HProcess); setPriorityClass (hprocess, high_priority_class); CloseHandle (HProcess);

Now we use the function TerminateProcess () termination process:

COUT << Endl << "Enter Process ID to Terminate That Process:"; CIN >> Processid; HProcess = OpenProcess (Process_Access, True, ProcessID); if (HProcess == Null) {cout << "Unable to get Handle of "<< processid; coup <<" error is: "<< getLastError ();} terminateprocess (hprocess, 0);

When we create objects through the New operator in the heap, you must explicitly delete it through Delete. DELETE processinfo; Return 0;

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

New Post(0)