#include#include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { PROCESSENTRY32 pe32; //Process entry32 structure pe32.dwSize = sizeof(PROCESSENTRY32); //Set structure size HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //Create snapshot if (Process32First(hSnapshot, &pe32) == TRUE) { //Find first process cout << "PID\tProcess Name" << endl; do { //Print process information cout << pe32.th32ProcessID << "\t" << pe32.szExeFile << endl; } while (Process32Next(hSnapshot, &pe32) == TRUE); } CloseHandle(hSnapshot); //Close snapshot return 0; }
#includeThis code uses the ProcEventInfo class to monitor process events, such as when a process terminates. It prompts the user to enter a process ID and continuously prints the process status until the process terminates. The package library used is the Windows API.#include #include #include #include using namespace std; void MonitorProcess(int processId) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId); //Open process if (hProcess != NULL) { DWORD exitCode = STILL_ACTIVE; while (exitCode == STILL_ACTIVE) { if (GetExitCodeProcess(hProcess, &exitCode)) { //Get process exit code cout << "Process " << processId << " status: " << exitCode << endl; } Sleep(1000); //Wait one second } CloseHandle(hProcess); //Close process handle } } int _tmain(int argc, _TCHAR* argv[]) { int processId; cout << "Enter the process ID: "; cin >> processId; if (processId > 0) { MonitorProcess(processId); //Monitor process } else { cout << "Invalid process ID" << endl; } return 0; }