如何调用exe静态方法并获取价值?

时间:2019-12-24 06:32:51

标签: c# command-line-interface exe

我的exe代码如下:

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

我的其他项目通过exe调用exe:

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();

我知道我可以设置args []做某事,但是我该如何调用某些方法并返回值

例如“ ADD(1 + 1)”

也许它可以发回消息或获取输出? ,我不知道。

3 个答案:

答案 0 :(得分:1)

您可以做的是..exe文件中的代码如下:

RequestOptions requestOption = new RequestOptions()
               .placeholder(R.drawable.boss)
               .circleCrop();

       Glide.with(context).load(imageURL)
               .transition(DrawableTransitionOptions.withCrossFade())
               .apply(requestOption)
               .into(view);

现在,在您的通话课中:

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

这将在控制台上打印3。

答案 1 :(得分:1)

有两种选择:

定义MyExe的命令行界面

另请参阅高拉夫萨的答案。

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;
        }
    }
}

在调用过程中,将“ ADD 1 1”作为命令行传递给MyExe.exe并从StdOutput读取结果(例如,参见Get Values from Process StandardOutput

将可执行文件用作类库

如果不需要在单独的过程中,则只需在项目中添加对Myexe.exe的引用,然后调用var result = Program.ADD(1, 2);

答案 2 :(得分:1)

您可以尝试这样的事情;流程执行:

  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);

Myexe必须读取命令行参数形式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)}");  
  }