title: PostBuild Event fails in Visual Studio after SignTool.exe error
body: <p>We have a project in Visual Studio 2010 that runs a batch file in the post-build event. That batch  calls to signtool.exe from Microsoft SDK to sign and timestamp the binary.</p>
  
  <p>Timestamp servers (we use <a href="http://timestamp.verisign.com/scripts/timstamp.dll" rel="nofollow noreferrer">http://timestamp.verisign.com/scripts/timstamp.dll</a>), however, tend to be unreliable for some reason, failing sometimes. This caused build to fail.</p>
  
  <p>We implemented a more advanced batch script then (based on <a href="https://stackoverflow.com/questions/2872105/alternative-timestamping-services-for-authenticode">this</a> code), splitting signing and timestamping, and allowing to retry the timestamp operation, if it failed.</p>
  
  <p>Here is a simplified version of the batch script (signfile.bat):</p>
  
  <pre><code>@echo off
  
  REM sign the file...
  signtool.exe /f Authenticode.pfx /p PASS %1
  
  if %errorlevel% neq 0 exit /b %errorlevel%
  
  set timestamp_server=http://timestamp.verisign.com/scripts/timstamp.dll
  
  for /L %%a in (1,1,10) do (
  
      REM try to timestamp the file...
      signtool.exe timestamp /t %timestamp_server% %1
  
      if errorlevel 0 if not errorlevel 1 GOTO succeeded
  
      REM wait 2 seconds...
      ping -n 2 127.0.0.1 &gt; nul
  )
  
  REM return an error code...
  echo signfile.bat exit code is 1.
  exit /b 1
  
  :succeeded
  REM return a successful code...
  echo signfile.bat exit code is 0.
  exit /b 0
  </code></pre>
  
  <p>And the post-build event code would look like:</p>
  
  <pre><code>signfile.bat "$(OutDir)$(TargetName)$(TargetExt)"
  </code></pre>
  
  <p>So, if the timestamping fails, it retries 10 times with 2-seconds intervals.</p>
  
  <p>But, what we observed was, if the timestamping went fine from the first attempt, everything was OK. However, if the first attempt failed, then the whole post-build event failed with code -1, even though the timestamping succeeded on the next try.</p>
  
  <blockquote>
  <pre><code>1&gt;------ Build started: Project: myproject, Configuration: NonOptimized x64 ------
  1&gt;  Done Adding Additional Store
  1&gt;  Successfully signed: E:\tfs\MySolution\bin\x64\NonOptimized\myproject.dll
  1&gt;  
  1&gt;EXEC : SignTool error : The specified timestamp server either could not be reached
  1&gt;  or returned an invalid response.
  1&gt;    This may happen if you specify an RFC 3161 timestamp URL but used
  1&gt;    the /t option or you specified a legacy Authenticode timestamp URL
  1&gt;    but used the /tr option.
  1&gt;EXEC : SignTool error : An error occurred while attempting to timestamp: E:\tfs\MySolution\bin\x64\NonOptimized\myproject.dll
  1&gt;  
  1&gt;  
  1&gt;  Number of errors: 1
  1&gt;  
  1&gt;  Successfully timestamped: E:\tfs\MySolution\bin\x64\NonOptimized\myproject.dll
  1&gt;  
  1&gt;  signfile.bat exit code is 0.
  1&gt;C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(113,5): error MSB3073: The command "signfile.bat "E:\tfs\MySolution\bin\x64\NonOptimized\myproject.dll"
  1&gt;C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(113,5): error MSB3073: :VCEnd" exited with code -1.
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  </blockquote>
  
  <p>So, as you can see, even though the error code returned from signfile.bat is 0, Visual Studio thinks it is -1 and fails the event.</p>
  
  <p>All attempts to clear the error flag, like adding <code>ver&gt;nul</code> here and there, or adding <code>exit 0</code> in the end (certainly with adding "call" before signfile.bat) didn't help since it seemed like Visual Studio checked not just for errorlevel but also for something else. In fact, the batch as well as signfile.bat only return 0 or 1 in case of error, but not -1. And if signtool.exe returns an error once, it seems like there is no way to convince Visual Studio not to fail the post-build event.</p>