In our system, we're monitoring a third-party executable which we have very little control over, lets call it TheServer.exe. I believe it's built in C++ Builder. Sometimes TheServer misbehaves and we need to kill and restart it. I'm writing some C# code to automate this, but when I start TheServer.exe using Process.Start()
, I get an error dialog from TheServer stating "External exception E0434F4D".
When starting TheServer from Explorer or the command line, there is no error. I also tried to start the process in debugging mode in Visual Studio 2010, also no error. Besides Process.Start
, I've tried the P/Invoke calls ShellExecute
and CreateProcess
with the same results. Is there some other way that I can start the process from .Net?
The code I'm using now:
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);
Edit: When finding this answer by Hans Passant, I created a very small C++ program as an intermediate.
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ShellExecuteA(NULL, "open", lpCmdLine, "", "", SW_NORMAL);
return 0;
}
Running this from the command line using Run.exe TheServer.exe
, the program starts without any errors. Running the same command line from .Net results in the same error dialog as before.
Edit: This question is quite similar to mine, but I do not use Xenocode Postbuild and I believe that was only part of the problem. But as suggested I tried stepping through my code, the exe then starts without error. Very strange indeed.