2012-12-18 16 views
7

Stworzyłem aplikację konsoli do sprawdzania poprawności funkcji i tę aplikację muszę wykonać przy użyciu vbscript. Po wykonaniu tego exe chcę zwrócić kod wyjścia, czy funkcja zwraca sukces, czy nie. Jak mogę zwrócić status lub kod zakończenia w .net?Jak powrócić kod wyjścia po uruchomieniu exe?

+0

Czy możesz pokazać swój kod? – gideon

+2

Ręcznie umieść Console.WriteLine w swoim kodzie i odczytaj wartość z Process.StandardOutput lub obsłużyć OutputDataReceived – Amitd

Odpowiedz

8

Założę się, że piszesz C# lub VB.NET. W obu przypadkach zwykle ludzie mają funkcję Główną, która niczego nie zwraca, , ale możesz to zmienić, aby zwrócić liczbę całkowitą reprezentującą kod wyjścia.

Dla C# patrz this MSDN page.

można zrobić:

static int Main() 
{ 
    //... 
    return 0; 
} 

Dla VB.NET zobaczyć this MSDN page.

można zrobić:

Module mainModule 
    Function Main() As Integer 
     '.... 
     '.... 
     Return returnValue 
    End Function 
End Module 
+0

Czy mogę uzyskać dostęp do głównej wartości zwracanej przez metodę za pomocą vbscript? – JEMI

+0

@JEMI Powinieneś być w stanie. Nie mogę powiedzieć dokładnie, jak bez twojego kodu. – gideon

+0

to jest kod static int Main (string [] args) { var ruleManager = new RuleManager(); int status = ruleManager.ExecuteRule(); status powrotu; } – JEMI

0

Jak @gideon skomentował w pliku wykonywalnego należy używać return oświadczenie powrót numer.

W swoim skrypcie przeczytaj %ERRORLEVEL% po wywołaniu tego pliku wykonywalnego. To miejsce, w którym Windows przechowuje kod powrotu.

4

Oprócz @gideon można także ustawić

Environment.ExitCode = theExitCode; 

W innych częściach kodu i wyjście bezpośrednio, jeśli coś naprawdę złego stało

0

Biorąc pod uwagę to C# program:

class MainReturnValTest { 
    static int Main(string[] args) { 
     int rv = 0; 
     if (1 == args.Length) { 
      try { 
       rv = int.Parse(args[0]); 
      } 
      catch(System.FormatException e)    { 
       System.Console.WriteLine("bingo: '{1}' - {0}", e.Message, args[0]); 
       rv = 1234; 
      } 
     } 
     System.Console.WriteLine("check returns {0}.", rv); 
     return rv; 
    } 
} 

Liczba próbek:

check.exe 
check returns 0. 

check.exe 15 
check returns 15. 

check.exe nonum 
bingo: 'nonum' Input string was not in a correct format. 
check returns 1234. 

i ten skrypt VBScript (zredukowane do minimum, nie rób tego w produkcji):

Option Explicit 

Const WshFinished = 1 


Dim goWSH : Set goWSH = CreateObject("WScript.Shell") 

Dim sCmd : sCmd = "..\cs\check.exe" 
If 1 = WScript.Arguments.Count Then sCmd = sCmd & " " & WScript.Arguments(0) 
WScript.Echo sCmd 
Dim nRet : nRet = goWSH.Run(sCmd, 0, True) 
WScript.Echo WScript.ScriptName, "would return", nRet 
With goWSH.Exec(sCmd) 
    Do Until .Status = WshFinished : Loop 
    WScript.Echo "stdout of check.exe ==>" & vbCrLf, .Stdout.ReadAll() 
    nRet = .ExitCode 
    WScript.Echo ".ExitCode of check.exe", nRet 
End With 
' !! http://stackoverflow.com/questions/2042558/how-do-i-get-the-errorlevel-variable-set-by-a-command-line-scanner-in-my-c-sha 
WScript.Echo "Errorlevel:", Join(Array(goWSH.Environment("PROCESS")("ERRORLEVEL"), goWSH.ExpandEnvironmentStrings("%ERRORLEVEL%"), "???"), " - ") 
WScript.Echo WScript.ScriptName, "returns", nRet 
WScript.Quit nRet 

Przykładowe trasy:

cscript 13921064.vbs 
..\cs\check.exe 
13921064.vbs would return 0 
stdout of check.exe ==> 
check returns 0. 

.ExitCode of check.exe 0 
Errorlevel: - %ERRORLEVEL% - ??? <=== surprise, surprise 
13921064.vbs returns 0 

echo %ERRORLEVEL% 
0 

cscript 13921064.vbs nonum & echo %ERRORLEVEL% 
..\cs\check.exe nonum 
13921064.vbs would return 1234 
stdout of check.exe ==> 
bingo: 'nonum' Input string was not in a correct format. 
check returns 1234. 

.ExitCode of check.exe 1234 
Errorlevel: - %ERRORLEVEL% - ??? 
13921064.vbs returns 1234 
0  <=== surprise, surprise 

DNV35 E:\trials\SoTrials\answers\13927081\vbs 
echo %ERRORLEVEL% 
1234 

Zobaczysz

  • WScript .Quit jest sposobem na zwrócenie kodu wyjścia ze skryptu.
  • Uruchamiasz inny proces za pomocą .Run lub .Exec
  • .Run zwraca kod zakończenia wywołanej procesem
  • .Exec ustawia .ExitCode (po ustaniu!)
  • Dostęp% ERRORLEVEL% w skrypcie jest daremny (@LexLi)
  • cscript 13921064.vbs nonum & echo %ERRORLEVEL% ma sensu zbyt
Powiązane problemy