0

在我们的系统中,我们正在监视一个我们几乎无法控制的第三方可执行文件,我们称之为 TheServer.exe。我相信它是在 C++ Builder 中构建的。有时 TheServer 行为不端,我们需要杀死并重新启动它。我正在编写一些 C# 代码来自动执行此操作,但是当我使用 启动 TheServer.exe 时Process.Start(),我从 TheServer 收到一个错误对话框,指出“外部异常 E0434F4D”。

从资源管理器或命令行启动 TheServer 时,没有错误。我还尝试在 Visual Studio 2010 中以调试模式启动该过程,也没有错误。此外Process.Start,我尝试了 P/Invoke 调用ShellExecuteCreateProcess结果相同。还有其他方法可以让我从.Net开始这个过程吗?

我现在使用的代码:

const string path = @"C:\Program files\TheServer\TheServer.exe";
ProcessStartInfo psi = new ProcessStartInfo()
{
  FileName = path,
  WorkingDirectory = Path.GetDirectoryName(path),
  UseShellExecute = true, // Tried false as well
};
Process.Start(psi);

编辑:当 Hans Passant 找到这个答案时,我创建了一个非常小的 C++ 程序作为中间程序。

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  ShellExecuteA(NULL, "open", lpCmdLine, "", "", SW_NORMAL);
  return 0;
}

从命令行使用 运行它Run.exe TheServer.exe,程序启动时没有任何错误。从 .Net 运行相同的命令行会导致与以前相同的错误对话框。


编辑:这个问题与我的非常相似,但我不使用 Xenocode Postbuild,我相信这只是问题的一部分。但正如建议的那样,我尝试单步执行我的代码,然后该 exe 启动而没有错误。确实很奇怪。

4

2 回答 2

0

这是我刚刚启动的记事本启动器。它工作正常

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Process process = new Process();
            process.StartInfo.FileName = "notepad";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;

            process.Start();
        }
    }
}
于 2012-11-12T15:33:33.923 回答
0

Oh, how silly. After a lot of tests and troubleshooting I was able to isolate the problem. It had nothing to do with Process.Start after all. When the we detect that the new process has started, we open a Telnet connection to it to query it's status. When that is done too early, the error message is displayed. Well, thanks for making me following the whole thing through.

于 2012-11-14T08:59:27.670 回答