Scan all processes of the system
1#include <stdio.h>2#include <windows.h>3#include <tlhelp32.h>4 5 intScan ()6 {7HANDLE Hprocessshap =NULL;8PROCESSENTRY32 pe32 = {0};9Hprocessshap = CreateToolhelp32Snapshot (Th32cs_snapall,0);Ten One if(Hprocessshap = =Invalid_handle_value) A { -printf"\ncreatetoolhelp32snapshot () failed:%d", GetLastError ()); - return 1; the } - -Pe32.dwsize =sizeof(PROCESSENTRY32); - + if(Process32First (Hprocessshap, &pe32)) - { + Do A { at wprintf (pe32.szexefile); -printf"\ n"); - } - while(Process32Next (Hprocessshap, &pe32)); - } - Else in { -printf"\nprocess32first () failed:%d", GetLastError ()); to } + CloseHandle (HPROCESSSHAP); - return 0; the } * $ intMainintargcChar*argv)Panax Notoginseng { - Scan (); the return 0; +}
gets the PID of a process
Sometimes when working on a process in a system, we need to get the PID of the program as a parameter. One of these methods is, of course, in the details of the task Manager, and the other is to get the PID of the process directly using the code.
1 intGetpid (wchar_t *name)2 {3HANDLE Hprocessshap =NULL;4 //The structure that holds the snapshot process information5PROCESSENTRY32 pe32 = {0}; 6 //Create a snapshot of the system's processes to scan7Hprocessshap = CreateToolhelp32Snapshot (Th32cs_snapall,0);8 if(Hprocessshap = =Invalid_handle_value)9 {Tenprintf"\ncreatetoolhelp32snapshot () failed:%d", GetLastError ()); One return 1; A } - //set the size of the structure body -Pe32.dwsize =sizeof(PROCESSENTRY32); the //Process32First get the handle to the first process - if(Process32First (Hprocessshap, &pe32)) - { - //the process name that matches the input + Do - { + if(!wcscmp (name, pe32.szexefile)) A { at return(int) Pe32.th32processid; - } - } - while(Process32Next (Hprocessshap, &pe32)); - } - Else in { -printf"\nprocess32first () failed:%d", GetLastError ()); to } + CloseHandle (HPROCESSSHAP); - return 0; the}
Scan system processes and get PID of a process