10-ReadProcess
We ll use previous knowledge to create a process then kill them with read process.
https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
#include <windows.h>
#include <stdio.h>
int main() {
// BOOL CreateProcessW(
// [in, optional] LPCWSTR lpApplicationName,
// [in, out, optional] LPWSTR lpCommandLine,
// [in, optional] LPSECURITY_ATTRIBUTES lpProcessAttributes,
// [in, optional] LPSECURITY_ATTRIBUTES lpThreadAttributes,
// [in] BOOL bInheritHandles,
// [in] DWORD dwCreationFlags,
// [in, optional] LPVOID lpEnvironment,
// [in, optional] LPCWSTR lpCurrentDirectory,
// [in] LPSTARTUPINFOW lpStartupInfo,
// [out] LPPROCESS_INFORMATION lpProcessInformation
// );
STARTUPINFOW si = {0};
PROCESS_INFORMATION pi = {0};
int process_id;
if(!CreateProcessW(
L"C:\\Windows\\System32\\notepad.exe",
NULL,
NULL,
NULL,
FALSE,
BELOW_NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&si,
&pi)) {
printf("Falied To create a process %ld",GetLastError());
}
else {
printf("Process Created - PID %ld\n", pi.dwProcessId);
process_id = pi.dwProcessId;
}
// HANDLE OpenProcess(
// [in] DWORD dwDesiredAccess,
// [in] BOOL bInheritHandle,
// [in] DWORD dwProcessId
// );
// OpenProcess(
// PROCESS_TERMINATE,
// FALSE,
// process_id
// );
/*
Other access levels you might use with OpenProcess include:
PROCESS_TERMINATE: Grants the right to terminate the process.
PROCESS_VM_READ: Grants the right to read the process's virtual memory.
PROCESS_VM_WRITE: Grants the right to write to the process's virtual memory.
PROCESS_VM_OPERATION: Grants the right to perform operations on the process's virtual memory, such as allocating or freeing memory.
PROCESS_SUSPEND_RESUME: Grants the right to suspend or resume the process.
*/
// We ll just terminate the process notepad we just created.
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, process_id);
if (hProcess == NULL) {
printf("Failed to open process handle: %ld\n", GetLastError());
return 1;
}
BOOL success = TerminateProcess(hProcess, 0);
if (!success) {
printf("Failed to terminate process: %ld\n", GetLastError());
} else {
printf("Process terminated successfully\n");
}
CloseHandle(hProcess);
return 0;
}