My winform application is launched by another application (the parent), I need determine the pid of the application which launch my application using C#.
3. Parent Process ID Spoofing. Parent PID Spoofing is a technique that allows attackers to run processes under any parent process they want.
The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.
The line PID = fork(); returns the value of the fork() system call. The if (PID == 0) evaluates the return value. If PID is equal to zero then printf() is executed in the child process, but not in the parent process.
WMI is the easier way to do this in C#. The Win32_Process class has the ParentProcessId property. Here's an example:
using System; using System.Management; // <=== Add Reference required!! using System.Diagnostics; class Program { public static void Main() { var myId = Process.GetCurrentProcess().Id; var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId); var search = new ManagementObjectSearcher("root\\CIMV2", query); var results = search.Get().GetEnumerator(); results.MoveNext(); var queryObj = results.Current; var parentId = (uint)queryObj["ParentProcessId"]; var parent = Process.GetProcessById((int)parentId); Console.WriteLine("I was started by {0}", parent.ProcessName); Console.ReadLine(); } }
Output when run from Visual Studio:
I was started by devenv
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