如何从exe获取返回值并重新启动它

发布于 2024-11-02 17:48:59 字数 374 浏览 3 评论 0原文

场景:我有一个MFC代码,它调用一个用C#创建的exe(它是一个Windows窗体应用程序)

需求:我需要exe在关闭时返回一个值,并且根据返回值,相同的 exe 将再次启动

psudocode

  int result = RunExe("exename", arguments)
  if(result == 1)
  {
     result =  RunExe("exename", arguments)
  }

我是否必须将 if 条件放入循环中?

请给我一些建议。 1.如何从exe返回值 2. 如何收取返回值 3.如何重新启动exe

Scenario: I have a MFC code which call an exe created in C# (it is a windows form application)

Need: I need that the exe would return a value when closed and on the basis of the return value the same exe will started again

psudocode

  int result = RunExe("exename", arguments)
  if(result == 1)
  {
     result =  RunExe("exename", arguments)
  }

do I have to put the if condition in loop?

plz give me some suggestion.
1.How to return a value from exe
2. How to collect the return value
3. How to restart the exe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅此而已 2024-11-09 17:48:59

您的 C# EXE 可以返回这样的 int 值:

[STAThread]
public static int Main() {
    return 5;
}

您的其他应用程序必须像此处其他应用程序所解释的那样处理返回值。

var proc = Process.Start("mycsharwinformapp.exe"):
proc.WaitForExit();

//If the code is 5 restart app!
if(proc.ExitCode==5) Process.Start("mycsharwinformapp.exe"): 

Your C# EXE can return an int value like this:

[STAThread]
public static int Main() {
    return 5;
}

Your other app has to handle the return value like the others here has explained.

var proc = Process.Start("mycsharwinformapp.exe"):
proc.WaitForExit();

//If the code is 5 restart app!
if(proc.ExitCode==5) Process.Start("mycsharwinformapp.exe"): 
洒一地阳光 2024-11-09 17:48:59

下面的方法应该可以解决问题;

private static int RunProcess(string processName, string arguments)
{
    Process process = new Process();
    process.StartInfo.FileName = processName;
    process.StartInfo.Arguments = arguments;
    process.Start();
    process.WaitForExit();
    return process.ExitCode;
}

然后像这样称呼它;

int returnCode;
do
{
    returnCode = RunProcess("...", "...");
}
while (returnCode == 1);

The following method should do the trick;

private static int RunProcess(string processName, string arguments)
{
    Process process = new Process();
    process.StartInfo.FileName = processName;
    process.StartInfo.Arguments = arguments;
    process.Start();
    process.WaitForExit();
    return process.ExitCode;
}

Then call it like so;

int returnCode;
do
{
    returnCode = RunProcess("...", "...");
}
while (returnCode == 1);
过去的过去 2024-11-09 17:48:59

您可以使用 process.ExitCode 并创建一个新的 EXE 来控制退出值并启动原始 EXE如果需要,或者如果信息大于整数,则将信息保存在磁盘上的文件中,以便您可以从父进程(您创建的新 EXE)处理它。

you can use the process.ExitCode and create a new EXE which controls the exitvalue and starts the original EXE if needed, or you save the information in a file on the disk if its more than an integer so you can process it from the parent process (the new EXE you create).

酷炫老祖宗 2024-11-09 17:48:59

就像OD所写的那样,Process.ExitCode是您正在寻找的值...

要启动进程,您可以使用Process.Start(string_path_to_exe,string_args)它将返回一个表示已启动进程的进程对象...要等待进程结束,请使用该对象的 WaitForExit() 方法,

请参阅进程类 @ MSDN

like O.D wrote, Process.ExitCode is the value you are looking for ...

to start the process you can use Process.Start(string_path_to_exe,string_args) which will return a Process object that represents the started process ... to wait until the process has ended use the WaitForExit() method of that object

see Process Class @ MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文