2

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.

4 Answers4

3

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

Ahmad Al Sayyed
  • 596
  • 5
  • 13
3

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"))
     {

     }
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • You cannot guarantee that no one run your process in this method, because the command args used to execute process A is accessible by anyone (you can see it from task manager also), check my answer below for more clarifications – Ahmad Al Sayyed Sep 16 '14 at 14:43
  • 2
    @Ahmad: there's really no point in doing anything more complicated than this; all we need to do is prevent the user from *accidentally* running the wrong executable. If the user is doing it on purpose, then (a) well, it's his computer, the programmer isn't entitled to stop him; and (b) the programmer *can't* stop him if he's determined enough. Failing all else, he could modify the code in the second executable to remove the checks. – Harry Johnston Sep 16 '14 at 23:26
2

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.

Community
  • 1
  • 1
Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
0

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.

Paul Weiland
  • 727
  • 10
  • 24