在 bat 文件中捕获 .net EXE 的返回值

发布于 2024-12-21 07:53:07 字数 445 浏览 4 评论 0原文

我当前正在运行一个 EXE 应用程序,该应用程序是从 bat 文件与其他一些 exe 调用的。我想做的是我想从 EXE 返回一个值并将其传递给 bat 文件中的另一个 EXE。我修改了控制台中的 Main 以将类型返回为整数,但无法访问 Bat 文件中的值,除了 ERRORLEVEL 中的值,我不想这样做,因为它可能会擦除任何潜在的错误。如果我使用 ERRORLEVEL 捕获返回值,例如发生错误并且返回错误整数,我如何区分 iam 返回的整数值和该值。请让我知道您对此的意见。

@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

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

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

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

发布评论

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

评论(2

時窥 2024-12-28 07:53:07

如果您的 EXE 当前不使用 stdout,那么您只需将结果写入 stdout,即可由父批处理进程捕获。

例如,如果要输出 EXE (myprog.exe)

var1=my first value
var2=23

,则可以使用这个简单的脚本来执行 EXE 并捕获和存储结果

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

即使正在使用 stdout,您也可以将结果添加到以下格式的输出中:这样您就可以解析出批次中所需的值。但如果 stdout 不方便,还有一个更简单的方法。

让您的 EXE 创建一个设置值的临时批处理文件。您的批处理文件可以在 EXE 完成后调用临时批处理文件,然后删除临时批处理文件。

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.

長街聽風 2024-12-28 07:53:07

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.

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