Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@RRUZ has already answered an <a href="https://stackoverflow.com/questions/1857037/determine-the-parent-process-of-the-current-app">almost identical question</a> on Stack Overflow. However, the code there is incorrect in that it declares process IDs as <code>THandle</code>. The following corrects the mistakes that I found, and also adapts the routine to return a PID rather than a filename:</p> <pre><code>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&lt;&gt;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&lt;&gt;0 then begin Result := ParentProcessId; CloseHandle(HandleParentProc); end; break; end; until not Process32Next(HandleSnapShot, EntryParentProc); end; CloseHandle(HandleSnapShot); end; end; </code></pre> <p>I know that this is a duplicate question, but the code here is precisely what the OP wants, so I'll leave it visible for a while at least.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload