-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid.cpp
More file actions
60 lines (48 loc) · 1.44 KB
/
pid.cpp
File metadata and controls
60 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @file pid.cpp
* @author Xuhua Huang
* @brief
* @version 0.1
* @date 2026-01-05
*
* @copyright Copyright (c) 2026
*
*/
#include <iomanip>
#include <iostream>
#include <string>
// clang-format off
#include <windows.h>
#include <tlhelp32.h>
// clang-format on
int main() {
// Take a snapshot of all processes in the system
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create snapshot." << std::endl;
return 1;
}
// Prepare the structure to hold process data
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
std::cout << std::left << std::setw(30) << "Process Name" << " | " << "PID" << std::endl;
std::cout << std::string(45, '-') << std::endl;
// Retrieve information about the first process
if (!Process32First(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return 1;
}
// Walk through the snapshot of processes
do {
// szExeFile is a WCHAR array in Unicode builds.
// We can wrap it in a std::wstring directly.
std::wstring processName(pe32.szExeFile);
// Use wcout for wide-character strings (Unicode)
std::wcout << std::left << std::setw(30) << processName << L" | " << pe32.th32ProcessID << std::endl;
} while (Process32Next(hSnapshot, &pe32));
// Clean up the handle
CloseHandle(hSnapshot);
std::cout << "\nScan complete. Press Enter to exit.";
std::cin.get();
return 0;
}