How to set ExitCode in a VCL Forms Application

泄露秘密 提交于 2019-12-05 00:29:41

Your method of setting the exit code is fine. It's your test that's faulty.

The shell doesn't wait for GUI applications to finish running before prompting for the next command. Thus, the error level has already been established. You're checking the value at the time the command prompt was displayed, not at the time you ran the echo command.

Running programs in a batch file or command script modifies the behavior of the command interpreter to make it wait for each command to finish before running the next one, even for programs marked as using the GUI subsystem instead of the console subsystem. That's why the error level is reported correctly from batch files — the process you ran had finished before the command interpreter fetches the exit code. Without using a command script, you can try starting your program with the start command and passing it the /wait option. I'm not sure it forwards the exit code of the process it starts, though.

You can establish the exit code like you're doing, but on the console you have to test the %errorlevel% variable in the same batch to get the value.

Instead of running your commands in the command prompt, create a simple bat like this:

REM calltest.bat

.\Test.exe
echo %ERRORLEVEL%

and then, invoke your test:

>calltest

I got this in my test:

>calltest.bat
>project3.exe
>echo 47

For both, setting directly the ExitCode variable or calling Halt.

My OS is Win7 64, if it makes any difference. Printing the %errorlevel% directly from the command line prints 0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!