Is there a way to find out how which process has started my process in c#?
I have two exes A.exe(console application) and B.exe(GUI). Now A.exe should only be started by B.exe and nothing else.
Any ideas are most welcomed.
Is there a way to find out how which process has started my process in c#?
I have two exes A.exe(console application) and B.exe(GUI). Now A.exe should only be started by B.exe and nothing else.
Any ideas are most welcomed.
You can add a special argument for A.exe and send it from B.exe such as process B ID (ProcessID) and check it when process A starts
for example:
From process B: A.exe 11233
where 11233 is process B ID
and in process A main(string[] args) check if process ID 11233 is for B.exe
Encrypt process ID for extra validation (to make sure no one runs your exe except your other process) because anyone can see the sent args used when process a was executed
Hope this helps
Basically you have a console application(A.exe) and GUI app(B.exe) and logic in A.exe can only be executed when it's executed through B.exe and not any other process or user.
You can implement it by passing an argument to A.exe,
Assume this how you call the A.exe from your GUI application,
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
//Set argument as "1"
startInfo.Arguments = "1";
startInfo.FileName = "A.exe path";
process.Start();
Within the console app's main method before executing any logic check if argument contains 1 to proceed.
static void Main(string[] args)
{
//Proceed only if argument contains "1"
if (null != args && args.Length > 0 && args[0].Equals("1"))
{
}
}
1st - process isn't started by user, it is always started by another process, and in that case you probably mean 'explorer.exe'.
And 2nd - what's wrong with this: How to get parent process in .NET in managed way ?
MORE:
Instead of passing out argument that is fixed and says I'm your master, I have a right to call you
, create one-time passwords, with each process knowing how to create the argument and read it. Its seed could be HH:MM:SS
and you should check for now and now-1 sec
, just in case that time wraps around just when you start it.
I`m not sure if this is what you need, but you could try to use Environment.StackTrace inside your method to see the call hierarchy in reversed order, meaning starting at the method you are inside going back to where your application started.
Description from MSDN:
The StackTrace property lists method calls in reverse chronological order, that is, the most recent method call is described first, and one line of stack trace information is listed for each method call on the stack. However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.
Hope this helps.