-2

我需要将输入参数传递给控制台应用程序并使用批处理文件检索输出参数。

步骤是:

  1. 使用输入参数运行控制台应用程序。(这是程序当前运行的方式。)

  2. 然后,我想等待控制台应用程序的响应,然后再继续下一步。该程序需要等待,因为它正在调用 Web 服务。我需要确定对 Web 服务的响应代码调用 = 0。

  3. 下一步取决于来自 Web 服务的响应。

    一个。如果来自 Web 服务的响应 = 0,则使用参数再次调用控制台应用程序以关闭 Web 服务上的客户帐户。

    同样在此步骤下,对控制台应用程序的第二次调用的结果应显示在用户可以看到的消息中。

    湾。如果来自 Web 服务的响应不 = 0,则应向用户显示一条消息,说明问题所在。没有进一步的调用。

因此,您能否向我展示代码和/或指出如何实现此目标的参考?

4

2 回答 2

1

让我们再试一次。这是应该做我认为你想要的批处理代码。

@echo off
call myApp.exe %*
set result=%errorlevel%
echo result = %result%

if %result%==0 call myApp.exe close (or whatever the paramaters are)
if %result%==1 echo "Message explaining what error code 1 means"
if %result%==2 echo "Message explaining what error code 2 means"
...

这将使用您传递到批处理文件中的任何参数调用 myApp.exe。如果需要,您可以将 替换为%*硬编码输入。然后打印应用程序的退出代码,并且可以再次调用它来关闭或打印一条解释错误代码的消息。


MSDN修改

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "externalExecutable.exe";
p.Arguments = "-arg1 -arg2";
p.Start();
// Wait for the child process to exit before
// reading to the end of its redirected stream.
p.WaitForExit();
// Read the output stream
string output = p.StandardOutput.ReadToEnd();
// Parse the output to see what you should do
// Or just use the exit code from the proccess
if (output.Equals("0") || p.ExitCode == 0) {
   // Do what you need to in this case
} else {
   // Do something else here
}
于 2013-04-22T22:06:20.290 回答
0

我已经在 vbnet 中完成了这个通用函数,真的不知道这是否会帮助你,因为我们不知道你在说哪种语言。

使用此函数,您可以等待并读取进程输出(例如进程 CMD):

#Region " Run Process Function "

    ' [ Run Process Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' MsgBox(Run_Process("Process.exe")) 
    ' MsgBox(Run_Process("Process.exe", "Arguments"))
    ' MsgBox(Run_Process("CMD.exe", "/C Dir /B", True))
    ' MsgBox(Run_Process("CMD.exe", "/C @Echo OFF & For /L %X in (0,1,50000) Do (Echo %X)", False, False))
    ' MsgBox(Run_Process("CMD.exe", "/C Dir /B /S %SYSTEMDRIVE%\*", , False, 500))
    '  If Run_Process("CMD.exe", "/C Dir /B", True).Contains("File.txt") Then MsgBox("File found")

    Private Function Run_Process(ByVal Process_Name As String, Optional Process_Arguments As String = Nothing, Optional Read_Output As Boolean = False, Optional Process_Hide As Boolean = False, Optional Process_TimeOut As Integer = 999999999)

        ' Returns True if "Read_Output" argument is False and Process was finished OK
        ' Returns False if ExitCode is not "0"
        ' Returns Nothing if process can't be found or can't be started
        ' Returns "ErrorOutput" or "StandardOutput" (In that priority) if Read_Output argument is set to True.

        Try

            Dim My_Process As New Process()
            Dim My_Process_Info As New ProcessStartInfo()

            My_Process_Info.FileName = Process_Name ' Process filename
            My_Process_Info.Arguments = Process_Arguments ' Process arguments
            My_Process_Info.CreateNoWindow = Process_Hide ' Show or hide the process Window
            My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
            My_Process_Info.RedirectStandardOutput = Read_Output '  Redirect (1) Output
            My_Process_Info.RedirectStandardError = Read_Output ' Redirect non (1) Output
            My_Process.EnableRaisingEvents = True ' Raise events
            My_Process.StartInfo = My_Process_Info
            My_Process.Start() ' Run the process NOW

            My_Process.WaitForExit(Process_TimeOut) ' Wait X ms to kill the process (Default value is 999999999 ms which is 277 Hours)

            Dim ERRORLEVEL = My_Process.ExitCode ' Stores the ExitCode of the process
            If Not ERRORLEVEL = 0 Then Return False ' Returns the Exitcode if is not 0

            If Read_Output = True Then
                Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
                Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)
                ' Return output by priority
                If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput ' Returns the ErrorOutput (if any)
                If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput ' Returns the StandardOutput (if any)
            End If

        Catch ex As Exception
            'MsgBox(ex.Message)
            Return Nothing ' Returns nothing if the process can't be found or started.
        End Try

        Return True ' Returns True if Read_Output argument is set to False and the process finished without errors.

    End Function

#End Region
于 2013-04-22T23:43:45.363 回答