@RRUZ는 이미 Stack Overflow에서 almost identical question으로 대답했습니다. 그러나 프로세스 ID를 THandle
으로 선언한다는 점에서 코드가 잘못되었습니다. 다음은 내가 찾은 실수를 수정하고, 또한 오히려 이름보다 PID를 반환하는 루틴을 적응 :
uses
Windows,
tlhelp32,
SysUtils;
function GetParentPid: DWORD;
var
HandleSnapShot: THandle;
EntryParentProc: TProcessEntry32;
CurrentProcessId: DWORD;
HandleParentProc: THandle;
ParentProcessId: DWORD;
begin
Result := 0;
HandleSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //enumerate the process
if HandleSnapShot<>INVALID_HANDLE_VALUE then
begin
EntryParentProc.dwSize := SizeOf(EntryParentProc);
if Process32First(HandleSnapShot, EntryParentProc) then //find the first process
begin
CurrentProcessId := GetCurrentProcessId; //get the id of the current process
repeat
if EntryParentProc.th32ProcessID=CurrentProcessId then
begin
ParentProcessId := EntryParentProc.th32ParentProcessID; //get the id of the parent process
HandleParentProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ParentProcessId);
if HandleParentProc<>0 then
begin
Result := ParentProcessId;
CloseHandle(HandleParentProc);
end;
break;
end;
until not Process32Next(HandleSnapShot, EntryParentProc);
end;
CloseHandle(HandleSnapShot);
end;
end;
내가이 중복 질문 알지만, 여기에 코드를 정확하게 OP가 원하는 것입니다, 그래서 나는 적어도 그것을 눈에 띄게 남겨 둘 것입니다.
링크를 보내 주셔서 감사합니다. –