How to modify return code in Visual Studio Build e

2020-03-15 07:34发布

问题:

Is it possible to modify the return code (I think it is also called errorlevel) of a command submitted in the Build events of Visual Studio?

I am running the command taskkill /F /IM MyApp.vshost.exe and I would like this command to return 0 when it is in fact returning 128.

回答1:

Redirect all output to a temp file and exit with code 0, in a batch file. This will effectively ignore any errors from taskkill:

killit.bat:

taskkill /F /IM MyApp.vshost.exe > %temp%\out.txt 2>&1
exit /B 0

Now invoke killit.bat in the build event.

Update After hege posted his answer I figured just pasting the code from the batch file into the build event should work as well, since to my understanding build events in VC are always executed on the command line anyway. And indeed

taskkill /F /IM MyApp.vshost.exe > %temp%\out.txt 2>&1 || exit /B 0

as a build event works as well. The redirection is still required though.



回答2:

Try taskkill /F /IM MyApp.vshost.exe || exit /b 0 .



回答3:

As seen here in the comment of the accepted answer:
Solve "The command "taskkill /F /IM MyApp.vshost.exe" exited with code 128" error

taskkill /F /IM MyApp.vshost.exe /fi "pid gt 0"

taskkill with filter returns no error.