当前位置: 动力学知识库 > 问答 > 编程问答 >

Capture return value of an .net EXE in an bat file

问题描述:

I am currently running an EXE application which is called from a bat file with some other exes. What iam trying to do is i want to return a value from EXE and pass it to an other EXE in bat file. I modified Main in my console to return type as an integer but unable to access value in Bat file except in ERRORLEVEL which i dont want to do as it may erase any potential errors. If i use ERRORLEVEL to capture return value for instance an error occured and it returns a error integer, how can i distinguish between the integer value which iam returning and this value. Please let me know your input on this.

@echo off

set PATH=%PATH%;C:\Scripts

Bat File:

TestConsoleApplication.exe

ECHO %ERRORLEVEL% -- Want to capture here return value of EXE dont want to do it using ERRORLEVEL

网友答案:

Errorlevel will have the return value of the program you ran. You can't specify to not have it hold the return value of the program.

If you are worrying about other errors elsewhere in your script, you can check the errorlevel at those places and store the value in another variable or jump to a particular label that handles that error.

网友答案:

If your EXE does not currently use stdout then you can simply write the results to stdout where it can be captured by the parent batch process.

For example, if your EXE (myprog.exe) were to output

var1=my first value
var2=23

then this simple script could be used to execute the EXE and capture and store the results

for /f "delims=" %%a in ('myprog.exe') do set "%%a"

Even if stdout is being used, you might be able to add the results to the output in such a way that you can parse out the values you need within the batch. But there is a simpler method if stdout is not convenient.

Have your EXE create a temporary batch file that sets the values. Your batch file can then call the temporary batch file after EXE completes and then delete the temp batch file.

分享给朋友:
您可能感兴趣的文章:
随机阅读: