1. Creating a Process
#include <windows.h> #include <tchar.h> #include <iostream>
int main() { STARTUPINFO si; PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi));
// Set command line parameters, here using notepad.exe as an example TCHAR cmdLine[] = _T("C:\Windows\System32\notepad.exe");
// Create process if (!CreateProcess(NULL, // Not using module name cmdLine, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance flag 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's drive and directory &si, // Pointer to STARTUPINFO structure &pi) // Pointer to PROCESS_INFORMATION structure ) { std::cerr << "CreateProcess failed (" << GetLastError() << ")." << std::endl; return 1; }
// Wait for the process to finish so we can see the notepad window WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread);
return 0;}
2. Getting the PID of a Specific Process
#include <windows.h> #include <tlhelp32.h> #include <iostream> #include <string>
DWORD GetProcessIdByName(const std::wstring& processName) { DWORD pid = 0; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hSnapshot, &pe32)) { do { if (std::wstring(pe32.szExeFile) == processName) { pid = pe32.th32ProcessID; break; } } while (Process32Next(hSnapshot, &pe32)); } CloseHandle(hSnapshot); } return pid;}
int main() { std::wstring processName = L"notepad.exe"; // Process name to find DWORD pid = GetProcessIdByName(processName); if (pid != 0) { std::wcout << L"Process ID for " << processName << L" is: " << pid << std::endl; } else { std::wcout << L"Process not found." << std::endl; } return 0;}
Process ID for notepad.exe is: 9428
3. Terminating a Process
#include <windows.h> #include <tlhelp32.h> #include <iostream>
bool CloseProcessByPID(DWORD pid) { HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (hProcess == NULL) { std::cerr << "OpenProcess failed (" << GetLastError() << ")." << std::endl; return false; }
if (!TerminateProcess(hProcess, 999)) { // 999 is the exit code, can be customized std::cerr << "TerminateProcess failed (" << GetLastError() << ")." << std::endl; CloseHandle(hProcess); return false; }
CloseHandle(hProcess); return true;}
int main() { DWORD pid = 9428; // Replace with the PID of the process you want to close if (CloseProcessByPID(pid)) { std::cout << "Process with PID " << pid << " has been terminated." << std::endl; } else { std::cout << "Failed to terminate process with PID " << pid << "." << std::endl; } return 0;}
Process with PID 9428 has been terminated.
4. Enumerating Processes
#include <windows.h> #include <tlhelp32.h> #include <iostream> #include <vector>
bool EnumerateProcesses(std::vector<DWORD>& processIds) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot == INVALID_HANDLE_VALUE) { return false; }
PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); if (!Process32First(hSnapshot, &pe32)) { CloseHandle(hSnapshot); return false; }
do { processIds.push_back(pe32.th32ProcessID); } while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot); return true;}
int main() { std::vector<DWORD> processIds; if (EnumerateProcesses(processIds)) { std::cout << "Enumerated processes:\n"; for (const auto pid : processIds) { std::cout << "PID: " << pid << std::endl; } } else { std::cerr << "Failed to enumerate processes." << std::endl; } return 0;}
Enumerated processes:
PID: 0
PID: 4
PID: 100
PID: 432
PID: 704
PID: 808
PID: 832
PID: 904
PID: 912

Want to learn more
Quickly scan the code to follow