Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify return code in Visual Studio Build events

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.

like image 909
Otiel Avatar asked Oct 27 '11 07:10

Otiel


People also ask

How to use post build event in Visual Studio?

In the Post-build event command line box, specify the syntax of the build event. Add a call statement before all post-build commands that run . bat files. For example, call C:\MyFile.

How do I debug a post build event in Visual Studio?

Another way is to check the bin\debug dir for 'PreBuildEvent. bat' or 'PostBuildEvent. bat' which are the file that Visual Studio creates and run during the build events, if there is an error the files remain in the output dir and you can run them manually and spot the error.

How do I open a build event in Visual Studio?

Go to Solution Explorer and right-click on the project then select Properties then go to the Build Events tab.

What is PreBuildEvent?

Pre/Post build events are useful when we wish to perform some operation before/after a project is built. These operations are nothing but the Shell commands being used from the command line. A build event can be formed using a single, multiple, or conditional commands. Introduction.


1 Answers

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.

like image 72
stijn Avatar answered Oct 26 '22 07:10

stijn