Experts Exchange connects you with the people and services you need so you can get back to work.
function TForm1.GetProcessParent(): string;
isparent := False;
//create snapshot of all processes
snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snap <> INVALID_HANDLE_VALUE then
pe32.dwSize := SizeOf(pe32);
//walk through processes to find the current process info
if Process32First(snap, pe32) then
currprocid := GetCurrentProcessId(); //store current process id for faster access
if pe32.th32ProcessID = currprocid then
//we've found the current process info
now we should compare current process start time with the parent process
start time, to check if the parent process CAN be the real parent
(process identifiers are reusable, so when the parent process terminates,
any other process started later on can get the same ID)
GetProcessTimes(GetCurrentProcess(), time_create, time_exit, time_kernel, time_user);
time_currentproc := time_create;
parentprocid := pe32.th32ParentProcessID;
parentproc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, parentprocid);
if parentproc <> 0 then
if GetProcessTimes(parentproc, time_create, time_exit, time_kernel, time_user) then
isparent := CompareFileTime(time_create, time_currentproc) < 0;
now we determined, that parentprocess CAN BE
the parent of the current process
SetLength(parentpath, 1024); //1024 should be enough for the full path :)
GetModuleFileNameEx(parentproc, 0, PChar(parentpath), 1024);
parentpath := PChar(parentpath);
break; //do not enumerate any further
until not Process32Next(snap, pe32);
if IsParent then
result := ParentPath
result := '';
Open in new window
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.
Premium members get this course for $159.20.
Premium members get this course for $12.50.
Premium members get this course for $148.75.
Premium members get this course for $79.20.
Premium members get this course for $167.20.