9

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.

Otiel
  • 18,404
  • 16
  • 78
  • 126

3 Answers3

15

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.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
stijn
  • 34,664
  • 13
  • 111
  • 163
  • Hmm, I was afraid I had to use a file to do that. What is `2>&1` for? – Otiel Oct 27 '11 at 08:36
  • redirects the standard error to standard output; if this is not used VS will still see the error description (if any) and trigger an error. Note that in your case it's probably better to pass MyApp.vshost.exe as an argument (something like $(OutName).vshost.exe) to the bat file, else you'd have to maintain one file per application. – stijn Oct 27 '11 at 09:14
  • This worked for me - was trying to start and stop a windows service everytime I built my web app because both the service and the web app shared a class library I was editing. Putting this in pre/post build events did the trick (swap the start with stop): sc stop WRQueueOutput > $(TargetDir)\ServiceStartStopOutput.txt 2>&1 || exit /B 0 – Chris Smith Mar 27 '13 at 18:28
2

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

hege
  • 987
  • 4
  • 15
1

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.

David Rettenbacher
  • 5,088
  • 2
  • 36
  • 45