title: How to modify contents / replace a binary of an .msi file as a post-build step?
body: <p>When building a Visual Studio 2010 Setup project with a CustomAction on x64 systems, Visual Studio includes the wrong version of <code>InstallUtilLib.dll</code>: It installs the 32bit shim, which will not work for CustomActions compiled as 64-bit (a requirement in my case, since it depends on 64-bit native dlls).</p>
  <p>Installing such a <code>.msi</code> results in the <code>System.BadImageFormat</code> exception.</p>
  <p>According to <a href="https://devblogs.microsoft.com/setup/64-bit-managed-custom-actions-with-visual-studio/" rel="nofollow noreferrer">this post (64-bit Managed Custom Actions with Visual Studio)</a>, the solution is to open the resulting <code>.msi</code> in <code>orca.exe</code> and replace the binary &quot;InstallUtil&quot;.</p>
  <p>I'd like to automate this. Any ideas?</p>
  <p><strong>EDIT:</strong> based on the answer provided by mohlsen, I added following script to the solution (not the setup project itself, as files added to the setup project go into the msi...):</p>
  <pre><code>Option Explicit
  rem -----------------------------------------------------------
  rem Setup_PostBuildEvent_x64.vbs
  rem 
  rem Patch an msi with the 64bit version of InstallUtilLib.dll 
  rem to allow x64 built managed CustomActions.
  rem -----------------------------------------------------------    
  
  Const msiOpenDatabaseModeTransact = 1
  Const msiViewModifyAssign         = 3
  
  rem path to the 64bit version of InstallUtilLib.dll
  Const INSTALL_UTIL_LIB_PATH = &quot;C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtilLib.dll&quot;
  
  Dim installer : Set installer = Wscript.CreateObject(&quot;WindowsInstaller.Installer&quot;)
  
  Dim sqlQuery : sqlQuery = &quot;SELECT `Name`, `Data` FROM Binary&quot;
  
  Dim database
  Set database = installer.OpenDatabase(Wscript.Arguments(0), msiOpenDatabaseModeTransact)
  Dim view : Set view = database.OpenView(sqlQuery)
  
  Dim record : Set record = installer.CreateRecord(2)
  record.StringData(1) = &quot;InstallUtil&quot;
  view.Execute record
  
  record.SetStream 2, INSTALL_UTIL_LIB_PATH
  
  view.Modify msiViewModifyAssign, record
  database.Commit
  
  Set view = Nothing
  Set database = Nothing
  </code></pre>
  <p>Next, I edited the Setup projects properties: I set the <code>PostBuildEvent</code> property to:</p>
  <pre><code>wscript.exe &quot;$(ProjectDir)\..\Setup_PostBuildEvent_x64.vbs&quot; $(BuiltOuputPath)
  </code></pre>
  <p><em>Note</em>: Right-clicking the setup project in solution explorer and then selecting &quot;Properties&quot; opens up the wrong dialog (&quot;Property Pages&quot;). You want the &quot;Properties Window&quot; (CTRL+W, P).</p>