views:

233

answers:

1

I am trying to integrate with a vendors app by calling it with command args from c#. It is meant to automate a process that we need to do with out needing anyone to interact with the application. If there are no errors when running the process it works fine.

However if there are any errors the vendors application will show a message box with the error code and error message and wait for someone to click the ok button. When the ok button is clicked it will exit the application returning the error code as the exit code.

As my application is going to be a windows service on a server, needing someone to click an okay button will be an issue. Just wondering what the best solution would be to get around this.

My code calling the vendor app is...

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "someapp.exe"
startInfo.Arguments = "somefile.txt";

Process jobProcess = Process.Start(startInfo);
jobProcess.WaitForExit();

int exitCode = jobProcess.ExitCode;
+1  A: 

Very quick, nasty and dirty code below. What you really want to do is have a loop that looks for a particular dialog box every second or and sends enter to it if it sees it. This should get you over the hill for now, however (untested, btw):

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "someapp.exe"
startInfo.Arguments = "somefile.txt";

Process jobProcess = Process.Start(startInfo);

//wait for the process to potentially finish...if it generally takes a minute to end, wait a minute and a half, etc
System.Threading.Thread.Sleep(60 * 1000);

Process[] processes = Process.GetProcessesByName("someapp");

if (processes.Length == 0)
{
  break; //app has finished running
}
else
{
  Process p = processes[0];  
  IntPtr pFoundWindow = p.MainWindowHandle;
  SetFocus(new HandleRef(null, pFoundWindow));
  SetForegroundWindow((int)pFoundWindow);
  SendKeys.SendWait("{ENTER}");          
}

int exitCode = jobProcess.ExitCode;

Option B is to modify their console app - find the MessageBoxA call and replace it with MOV EAX, 1 so that the dialog box never shows up and it's like the user clicked 'OK'.

szevvy
I had tried something like this, but it does not work when my app is running as a service.The MainWindowHandle on the process is zero.
holz
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/d4dd2efd-353e-4c9c-ab08-b6d264aa1c9dGive that a whirl.
szevvy