2012-04-17 2 views
0

가능한 중복을 처리하기 :
Determine the parent process of the current app주요 공정에서

나는 프로세스의 MainProcess 핸들 또는 PID를 좀하고 싶습니다. 예를 들어 Google 크롬은 실제로 스레드 인 각 탭에 대해 다른 프로세스를 삭제합니다. ProcessExplorer에서 chrome.exe를 주 프로세스 및 해당 스레드 아래의 스레드로 트리보기에 표시합니다. MainProcess Handle/PID를 어떻게 확인하거나 얻을 수 있습니까? WindowsAPI와 같은 것?

도움 주셔서 감사합니다.

+0

링크를 보내 주셔서 감사합니다. –

답변

3

@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가 원하는 것입니다, 그래서 나는 적어도 그것을 눈에 띄게 남겨 둘 것입니다.

+0

그게 내가 찾고있는 것입니다. 고맙습니다. :) –

+0

이후'GetModuleFileNameEx' 함수 호출을 제거하여 부모 프로세스 파일 이름을 얻게되면'Psapi' 유닛도 제거 할 수 있습니다. – RRUZ

+0

@RRUZ 고맙습니다. 끝난. 아마 SysUtils도 갈 수 있습니다. –