如何在C ++中按其名称获取进程句柄?

我正在尝试获取example.exe的进程句柄,因此可以对其进行调用TerminateProcess。我怎样才能做到这一点?请注意,它没有窗口,因此FindWindow无法使用。



繁星coding
浏览 1000回答 3
3回答

BIG阳

#include <cstdio>#include <windows.h>#include <tlhelp32.h>int main( int, char *[] ){&nbsp; &nbsp; PROCESSENTRY32 entry;&nbsp; &nbsp; entry.dwSize = sizeof(PROCESSENTRY32);&nbsp; &nbsp; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);&nbsp; &nbsp; if (Process32First(snapshot, &entry) == TRUE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (Process32Next(snapshot, &entry) == TRUE)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stricmp(entry.szExeFile, "target.exe") == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do stuff..&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hProcess);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; CloseHandle(snapshot);&nbsp; &nbsp; return 0;}另外,如果您想在OpenProcess中使用PROCESS_ALL_ACCESS,则可以尝试以下操作:#include <cstdio>#include <windows.h>#include <tlhelp32.h>void EnableDebugPriv(){&nbsp; &nbsp; HANDLE hToken;&nbsp; &nbsp; LUID luid;&nbsp; &nbsp; TOKEN_PRIVILEGES tkp;&nbsp; &nbsp; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);&nbsp; &nbsp; LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);&nbsp; &nbsp; tkp.PrivilegeCount = 1;&nbsp; &nbsp; tkp.Privileges[0].Luid = luid;&nbsp; &nbsp; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;&nbsp; &nbsp; AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);&nbsp; &nbsp; CloseHandle(hToken);&nbsp;}int main( int, char *[] ){&nbsp; &nbsp; EnableDebugPriv();&nbsp; &nbsp; PROCESSENTRY32 entry;&nbsp; &nbsp; entry.dwSize = sizeof(PROCESSENTRY32);&nbsp; &nbsp; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);&nbsp; &nbsp; if (Process32First(snapshot, &entry) == TRUE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (Process32Next(snapshot, &entry) == TRUE)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (stricmp(entry.szExeFile, "target.exe") == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do stuff..&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hProcess);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; CloseHandle(snapshot);&nbsp; &nbsp; return 0;}

萧十郎

有两种基本技术。第一种使用PSAPI;第二种使用PSAPI。MSDN有一个例子,使用EnumProcesses,OpenProcess,EnumProcessModules,和GetModuleBaseName。另一个使用我更喜欢的Toolhelp。使用CreateToolhelp32Snapshot来获取进程列表的快照,走在它与Process32First和Process32Next,它提供了模块名称和进程ID,直到你找到你想要的,然后调用OpenProcess得到的句柄。
打开App,查看更多内容
随时随地看视频慕课网APP