2012-04-17 5 views
0

可能性の重複を扱う取得:
Determine the parent process of the current appメインプロセスから

私は、プロセスのMainProcessハンドルやPIDを取得したいと思います。 たとえば、Google Chromeは、実際にスレッドであるタブごとに別のプロセスを削除します。 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も行くことができます。 –

関連する問題