0

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.

Community
  • 1
  • 1
Anlo
  • 3,228
  • 4
  • 26
  • 33
  • Sounds like a permissions issue. – leppie Nov 12 '12 at 11:10
  • I tried running my application as admin, same results. Is there something else I could try? – Anlo Nov 12 '12 at 11:13
  • Presumably you've tried running notepad.exe as a test? Are you sure your EXE can find all of it's dependencies (DLLs)? You could also try running your app under WinDBG and get it to http://stackoverflow.com/questions/771039/can-visual-studio-be-made-to-debug-child-processes-like-windbg – cirrus Nov 12 '12 at 11:59
  • Contact the owner of the EXE for support. Send him a test program that reproduces the problem. – Hans Passant Nov 12 '12 at 12:00
  • Running notepad works fine. I'm pretty sure the exe can find all dlls, running it from the install folder in Explorer works fine and I set the working directory when running it from my application. After the inital error dialog, the exe keeps running and seem to work ok. – Anlo Nov 12 '12 at 12:49
  • Unfortunately, asking the third party to fix the problem is not an option right now. When we ask them to fix a bug, they usually introduces a couple of new ones. So we have resolved to using workarounds. Since the exe starts up fine when running from Explorer or command line, I think it should be possible to run it from .Net as well. – Anlo Nov 12 '12 at 12:52
  • Maybe the VS2010 test app is running in the 64-bit address space and causes compatibility issues ? – pleinolijf Nov 12 '12 at 13:56
  • Good idea, but it was already set to x86. I tried x64 anyway, but got the same results. – Anlo Nov 12 '12 at 14:35

2 Answers2

0

Here's a notepad launcher that I just spun up. it works fine

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

Anlo
  • 3,228
  • 4
  • 26
  • 33