控制台应用程序显示错误消息框

发布于 2024-08-27 11:30:31 字数 561 浏览 4 评论 0原文

我正在尝试通过使用 c# 中的命令参数调用供应商应用程序来与它集成。它的目的是自动化我们需要执行的流程,而不需要任何人与应用程序交互。如果运行该过程时没有错误,则它可以正常工作。

但是,如果出现任何错误,供应商应用程序将显示一个包含错误代码和错误消息的消息框,并等待有人单击“确定”按钮。单击“确定”按钮后,它将退出应用程序,并返回错误代码作为退出代码。

由于我的应用程序将成为服务器上的 Windows 服务,因此需要有人单击“确定”按钮将是一个问题。只是想知道解决这个问题的最佳解决方案是什么。

我调用供应商应用程序的代码是...

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

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

int exitCode = jobProcess.ExitCode;

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;

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

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

发布评论

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

评论(1

少跟Wǒ拽 2024-09-03 11:30:31

下面是非常快速、肮脏和肮脏的代码。您真正想要做的是有一个循环,每秒查找一个特定的对话框,或者如果看到它就向它发送输入。不过,这应该能让你暂时摆脱困境(未经测试,顺便说一句):

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;

选项 B 是修改他们的控制台应用程序 - 找到 MessageBoxA 调用并将其替换为 MOV EAX, 1 以便对话框永远不会显示,就像用户单击“确定”。

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'.

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