Leszek
Leszek

Reputation: 71

Passing parameters to an EXE assembly

I'd like to launch my application from another utility app and pass a parameter. The simplest solution:

System.Diagnostics.Process.Start(appPath, param);

... is not an option as I'd like to prevent other people from providing the parameter to the app. In other words: I'd like to lauch my application with a param only from my utility app.

How to achieve that?

Thanks, Leszek

Upvotes: 4

Views: 656

Answers (4)

Leszek
Leszek

Reputation: 71

Another option that came to my mind is to accept the parameter only in the Debug build. I have control over both applications and I'm the only one who needs to lauch the main application with a param. I could strip this functionality off in the release build.

Upvotes: 0

Steve
Steve

Reputation: 216243

If you (for security reason) don't want to pass meaningful parameters to start your main program then you can use Anonymous Pipes (starting from Net 4)
Basically you follow this pseudocode

  • From the utility, create the anonymous pipe and start your main program passing the handle of the pipe.
  • The main program use the handle passed to listen on the anonymous pipe
  • The utility program send the message using the anonymous pipe

Here you can find a detailed example

In this way, there is a parameter passed from the utility to the main program representing the anonymous pipe, but no other program or user can easily fake the main program. If you add to this scenario also some form of secret handshaking between the two programs you will get a robust protection.

Upvotes: 2

Jared Harley
Jared Harley

Reputation: 8337

If I understand correctly, you're wanting the process to go like this:

  1. Start UtilityProgram.exe
  2. Do something in UtilityProgram that starts YourApplication.exe

And you want to make sure no one else can start YourApplication.exe outside of the UtilityProgram.

I think the simplest way would be to require a parameter in YourApplication.exe that is complex, sort of like a password.

So, in YourApplication.exe, your code could look like this:

static int Main(string[] args){
    if (args.Length != 1) {
        // We got more than 1 parameter, exiting with a code of -1.
        return -1;
    }

    if (args[0] != "abc123def456ghi") {
        // We got 1 parameter, but it wasn't right, exiting with a code of -2.
        return -2;
    }

    // Got 1 matching parameter
    // Code goes here
}

And then in UtilityProgram.exe, you can do this:

Process MyApp = new Process();
MyApp.StartInfo.FileName = @"C:\path\to\MyApp\exe";
MyApp.StartInfo.Arguments = "abc123def456ghi";
MyApp.Start();

Upvotes: 1

malkassem
malkassem

Reputation: 1957

You can make one of your app's parameters a password that you only know. Then you can pass that parameter from the launching utiliy.

Upvotes: 1

Related Questions