I wrote this utility (exe) which can be called from within this host application. And I'd prefer it that the utility can only be called from the host application. Running it from the outside or a different host should terminate the utility immediately.
Is there a way to find out which process launched my utility?
Thanks for the replies.
To determine the parent process of a specific process, we use the ps command. The output only contain the parent process ID itself. Using the output from the ps command we can determine the name of the process.
Just as in UNIX based system the init process is the parent of all the processes and similarly, the zygote process in Android OS has the sole purpose of launching other processes.
bat ). Typically, when a user runs a command prompt, the parent process is explorer.exe or another instance of the prompt. There may be automated programs, logon scripts, or administrative tools that launch instances of the command prompt in order to run scripts or other built-in commands.
Type the simply “pstree” command with the “-p” option in the terminal to check how it displays all running parent processes along with their child processes and respective PIDs. It shows the parent ID along with the child processes IDs.
You can use the CreateToolhelp32Snapshot function to enumerate the running process list and then the Process32First function to get the th32ParentProcessID which is the identifier of the process that created this process (its parent process).
See this example .
uses
Psapi,
Windows,
tlhelp32,
SysUtils;
function GetTheParentProcessFileName(): String;
const
BufferSize = 4096;
var
HandleSnapShot : THandle;
EntryParentProc : TProcessEntry32;
CurrentProcessId: DWORD;
HandleParentProc: THandle;
ParentProcessId : DWORD;
ParentProcessFound : Boolean;
ParentProcPath : String;
begin
ParentProcessFound := False;
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
ParentProcessFound := True;
SetLength(ParentProcPath, BufferSize);
GetModuleFileNameEx(HandleParentProc, 0, PChar(ParentProcPath),BufferSize);
ParentProcPath := PChar(ParentProcPath);
CloseHandle(HandleParentProc);
end;
break;
end;
until not Process32Next(HandleSnapShot, EntryParentProc);
end;
CloseHandle(HandleSnapShot);
end;
if ParentProcessFound then
Result := ParentProcPath
else
Result := '';
end;
I would add an extra parameter that you only know about ( it can be a guid ) if that guid is not part of the parameters passed to your utility .. Terminate the application right away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With