Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a WPF application in C# and it starts from a very simple 'launcher' exe that starts the main application along with all of the support services and filesystem watchers as separate exe's. The main application cannot function without most of its services so I want to know the source of the startup of just the main application if it can be done. Is there any way that I can tell if the main application exe was started from windows explorer (or any other source) as a stand alone file or if it was started from the 'launcher' process (I will be using Process.Start();) For example, if the process was started from its exe file then I will display a message telling the user that they must use the launcher to start it. Or will I just have to detect if the relevant services are running and use that? Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 14-Nov-13 14:29pm    
This is not a "source", this is called "parent process". And it's not "Windows Explorer", it's the Windows Shell. The real solution would be: you don't need to know this process...
—SA

In principle, you get information on the parent process (like "launcher.exe"), using P/Invoke (see, for example, http://spradip.wordpress.com/2008/10/24/getting-parent-process-id-from-child-without-passing-any-arguments-2/[^]) or in a managed way (see, for example, http://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way[^]).

But what can it give you? You still need to tell which of the possible parent processes you current parent is, and, for this purpose, you would need to pass some identification information (such as PID) to the child process (application), and this will, in turn, require some IPC mechanism. Even if you reduce the problem to comparison of the parent process name (if you have the PID, you can traverse all current process using System.Diagnostics.Process.GetProcesses() and find then name by known PID), this would not be a dirty method, because there is some chance of having some unrelated process with the same name; and, in general, having any hard-coded names in the application is dirty programming.

So, does it even worth bothering? I don't think so. If you already have to pass some information to the child process via IPC, it would be much better to directly give this process information on its environment.

You really need to modify this "launcher.exe" (or whatever it is), to tell the child process information on the runtime environment (whatever it is). In the simplest variant, you can define a command-line parameter in your application which tells it something about the environment. There are many other ways to pass some data. Please see:
http://en.wikipedia.org/wiki/Inter-process_communication[^].

And, finally, if, by any chance, you need maximum flexibility or advanced featured of hosting of your application, you can have a custom CLR host. Please start here:
http://msdn.microsoft.com/en-us/library/dd380850%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
In addition to the solution 1, I have a completely different idea, which might be much better.

You see, many students are taught to use defensive style of programming, but in many, many cases it turns to be a bad idea. In modern high-tech programming, offensive programming should dominate, especially when technologies like structural exception handling or, say, transactions, became a commonplace.

The idea is: you mentioned that your application, when started from the parent process like "launcher.exe", uses some "support services". Perhaps using the application without those support services does not makes sense, but, even this is not the case, this should not be a limitation. You should simply try to unconditionally use one of these "support services" in the very beginning of the application runtime (try to connect to the service, or something like that), do it under the try-catch block, and legitimately fail in case that support service is not started. If the failure happens, the application catching appropriate exception either exits immediately, or continue running according to your "started directly from the Shell" scenario, when the supporting services can not be used.

If you think about it, it would be the most reasonable approach, and, at the same time, the easiest to implement and most efficient, reliable and maintainable. Well, honestly. :-)

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900