I'm trying to get the process handle of, say example.exe, so I can call TerminateProcess
on it. How can I do this? Notice, it doesn't have a window so FindWindow
won't work.
Use CreateToolhelp32Snapshot to get a snapshot of the process list, walk over it with Process32First and Process32Next , which provides module name and process ID, until you find the one you want, and then call OpenProcess to get a handle.
A process handle is an integer value that identifies a process to Windows. The Win32 API calls them a HANDLE; handles to windows are called HWND and handles to modules HMODULE. Threads inside processes have a thread handle, and files and other resources (such as registry keys) have handles also.
In C and C++, you can call the getpid() library function which is a function from the POSIX library. #include <sys/types. h> #include <unistd. h> pid_t getpid(void);
You can check whether the exit code is STILL_ACTIVE and if so, you can call WaitForSingleObject(processHandle, 0) and check whether the return value is WAIT_TIMEOUT . If so, the process is still active, otherwise the process has returned 259 as exit code.
#include <cstdio> #include <windows.h> #include <tlhelp32.h> int main( int, char *[] ) { PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (Process32First(snapshot, &entry) == TRUE) { while (Process32Next(snapshot, &entry) == TRUE) { if (stricmp(entry.szExeFile, "target.exe") == 0) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); // Do stuff.. CloseHandle(hProcess); } } } CloseHandle(snapshot); return 0; }
Also, if you'd like to use PROCESS_ALL_ACCESS in OpenProcess, you could try this:
#include <cstdio> #include <windows.h> #include <tlhelp32.h> void EnableDebugPriv() { HANDLE hToken; LUID luid; TOKEN_PRIVILEGES tkp; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid); tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = luid; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL); CloseHandle(hToken); } int main( int, char *[] ) { EnableDebugPriv(); PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (Process32First(snapshot, &entry) == TRUE) { while (Process32Next(snapshot, &entry) == TRUE) { if (stricmp(entry.szExeFile, "target.exe") == 0) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); // Do stuff.. CloseHandle(hProcess); } } } CloseHandle(snapshot); return 0; }
The following code shows how you can use toolhelp and OpenProcess to get a handle to the process. Error handling removed for brevity.
HANDLE GetProcessByName(PCSTR name) { DWORD pid = 0; // Create toolhelp snapshot. HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 process; ZeroMemory(&process, sizeof(process)); process.dwSize = sizeof(process); // Walkthrough all processes. if (Process32First(snapshot, &process)) { do { // Compare process.szExeFile based on format of name, i.e., trim file path // trim .exe if necessary, etc. if (string(process.szExeFile) == string(name)) { pid = process.th32ProcessID; break; } } while (Process32Next(snapshot, &process)); } CloseHandle(snapshot); if (pid != 0) { return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); } // Not found return NULL; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With