4

I have an AutoUpdater application that launches another application. Is there anyway to force that second application to ONLY run if it was started by the AutoUpdater?

The problem is, some end-users will create a shortcut to the main app file on their desktop. This becomes a problem because it's supposed to check for updates to the app before it launches, and they don't receive our updates.

One thought I had was to create an IPC channel via WCF and issue a very simple command from the AutoUpdater to the other application. If the other app doesn't receive that command within 2 or 3 seconds it would close itself.

This seems like more code/overhead than what should be needed though. Is there an easier way?

Thanks!

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • 1
    I did a quick search and found this blog post (not my blog) that had a possible solution. http://www.rhyous.com/2010/04/30/how-to-get-the-parent-process-that-launched-a-c-application/ I didn't have time to test it out so I didn't post it as an answer but it might get you headed in the right direction. – jfrankcarr May 17 '13 at 22:33
  • @jfrankcarr Thanks a lot. This is very helpful. Just for simplicity sake, I think for now I'm going to do the command line argument approach, but I'm bookmarking this article and may visit that solution later when I have more time. – Adam Plocher May 17 '13 at 23:19
  • 1
    Have you tried checking for the parent process and bailing if its not what you expect? http://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way – devshorts May 18 '13 at 02:48

2 Answers2

6

Windows forms applications also have a main method, which can take parameters. You could read some parameter, and if it doesn't conform to your rules, you can either keep the form from opening (therefore the user wouldn't see a thing), or you can chastise the user, I mean, give a message that they shouldn't open your app that way. I think this is simpler than using WCF.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
2

You can run your App by using

System.Diagnostics.Process.Start("", "TestString");

The first string is the path of App you want to start, something like "C:/AppName.exe" and the second string is any text you want but it must be the same string that you check in the 'if' below. In the App you need to check the text given trough the second string.

static void Main(string[] args)
{
    if(!args.Length == 1 || !args[0]=="TestString") Environment.Exit(0);
    //↑ lenght check to avoid exception, then check for the string itself
    //the OS gives the string to args, you don't need to take care about that

    //rest of your code
}

I hope I helped to solve it.

Preza8
  • 400
  • 1
  • 3
  • 13