C ++에서 이름으로 프로세스 핸들을 얻으려면 어떻게해야합니까?
예를 들어 example.exe의 프로세스 핸들을 가져 오려고하므로 호출 할 수 있습니다 TerminateProcess
. 어떻게 할 수 있습니까? 창이 없으므로 FindWindow
작동하지 않습니다.
#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;
}
또한 OpenProcess에서 PROCESS_ALL_ACCESS를 사용하려면 다음을 시도하십시오.
#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;
}
다음 코드는 toolhelp 및 OpenProcess를 사용하여 프로세스에 대한 핸들을 가져 오는 방법을 보여줍니다. 간결성을 위해 오류 처리가 제거되었습니다.
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;
}
두 가지 기본 기술이 있습니다. 첫 번째는 PSAPI를 사용합니다. MSDN을 갖는 일례 그 용도 EnumProcesses
, OpenProcess
, EnumProcessModules
, 및 GetModuleBaseName
.
The other uses Toolhelp, which I prefer. 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.
Check out: MSDN Article
You can use GetModuleName
(I think?) to get the name and check against that.
OpenProcess Function
From MSDN:
To open a handle to another local process and obtain full access rights, you must enable the SeDebugPrivilege privilege.
The following code can be used:
DWORD FindProcessId(const std::wstring& processName)
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (processesSnapshot == INVALID_HANDLE_VALUE) {
return 0;
}
Process32First(processesSnapshot, &processInfo);
if (!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while (Process32Next(processesSnapshot, &processInfo))
{
if (!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
Usage:
auto processId = FindProcessId(L"blabla.exe");
Getting a handle should be obvious, just call OpenProcess()
or similar on it.
ReferenceURL : https://stackoverflow.com/questions/865152/how-can-i-get-a-process-handle-by-its-name-in-c
'programing' 카테고리의 다른 글
Xcode에서 "URL 유형"을 삭제하는 방법은 무엇입니까? (0) | 2021.01.14 |
---|---|
열 필드에 대해 두 행의 차이를 얻는 방법은 무엇입니까? (0) | 2021.01.14 |
TextView의 maxLines와 ellipsize를 동시에 설정하는 방법 (0) | 2021.01.14 |
Android의 인 텐트 필터 란 무엇입니까? (0) | 2021.01.14 |
Objective C에서 속성 이름 앞에 밑줄 추가 (0) | 2021.01.14 |