0

My exe code like this:

class Program
    {
        //here I want to call
        static int ADD(int a , int b){return a+b};
        static void Main(string[] args)
        {
        }
    }

My other project call exe by Progress like :

Process p = new Process();
                p.StartInfo.Arguments = sb.ToString();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.FileName = "Myexe.exe";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.EnableRaisingEvents = true;
                p.StartInfo.CreateNoWindow = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardInput = true;
                p.Start();

I knew I can set args[] to do something , But how can I invoke some method & return value ,

for example "ADD(1+1)"

Maybe it can send message back or get output? , I don't know.

Compo
  • 36,585
  • 5
  • 27
  • 39
TimChang
  • 2,249
  • 13
  • 25

3 Answers3

1

What you can do is this..In your exe you have code like this:

public class CalledExe
{
    public static void ADD(int a, int b) { Console.WriteLine( a + b); }
    public static void Main(string[] args)
    {
        int a, b;

        if (args == null && !args.Any())
            return;

        if (args.Count() != 2)
            return;

        if (args.Count() == 2)
            if (int.TryParse(args[0], out a) && int.TryParse(args[1], out b))
                ADD(a, b);

    }//end void function Main

}//end CalledExe class

Now, in your calling class:

public class CallingExe
{
   public static void main(string[] args)
   {
      System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.StartInfo.FileName = @"CalledExe.exe";
      proc.StartInfo.Arguments = "1 2";
      proc.Start();
   }
}

This will print 3 on the console.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
1

There are two opions:

Define a command line interface to MyExe

See also Gauravsa's answer.

class Program
{
    static int ADD(int a , int b){return a+b};
    static void Main(string[] args)
    {
        // error handling omitted!
        switch (args[0])
        {
        case "ADD":
            Consle.WriteLine("={0}", ADD(int.Parse(args[1]), int.Parse(args[2)));
            break;
        }
    }
}

In the calling process, pass "ADD 1 1" to MyExe.exe as command line and read the result from StdOutput (see e.g. Get Values from Process StandardOutput)

Use the executable as class library

If it does not need to be in a separate process, you can simply add a reference to Myexe.exe to your project and then call var result = Program.ADD(1, 2);.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
1

You can try something like this; process execution:

  string result; 

  ProcessStartInfo info = new ProcessStartInfo() {
    EnableRaisingEvents = true, 
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    Arguments = "1 2", // we pass 2 arguments: 1 and 2
    FileName = @"Myexe.exe",
  };

  using (Process process = new Process()) {
    process.StartInfo = info;
    process.Start();

    StringBuilder sbOut = new StringBuilder();
    StringBuilder sbErr = new StringBuilder();

    process.OutputDataReceived += (sender, e) => {
      if (e.Data != null) {
        sbOut.AppendLine(e.Data);
      }
    };

    process.ErrorDataReceived += (sender, e) => {
      if (e.Data != null)
        sbErr.AppendLine(e.Data);
    };

    process.BeginErrorReadLine();
    process.BeginOutputReadLine();

    // Let process complete its work
    process.WaitForExit();

    result = sbErr.Length > 0 
      ? sbErr.ToString()   // process failed, let's have a look at StdErr
      : sbOut.ToString();  // process suceeded, let's get StdOut
  }

  Console.WriteLine(result);

When Myexe has to read form command line arguments (args):

  static void Main(string[] args) {
    // When given args we parse them...  
    if (args.Length >= 2 && 
        int.TryParse(args[0], out int a) && 
        int.TryParse(args[1], out int b)) 
      Console.WriteLine(a + b); // and return (via StdOut) their sum
    else
      Console.WriteLine($"Failed to execute {string.Join(" ", args)}");  
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215