title: An attempt was made to load a program with an incorrect format (.dll)
body: <p>My program compiles nicely, but when I run the program I get this error: </p>
  
  <blockquote>
    <p>"System.BadImageFormatException thrown calling IO_Init(). Message=An
    attempt was made to load a program with an incorrect format.
    (Exception from HRESULT: 0x8007000B)"</p>
  </blockquote>
  
  <p>As far as I understand, this error is because my program tries to use a dll which is not the same bitness as my program (which is compiled in 64-bit). I have a 32-bit version of the dll as well, and when I run the 32-bit version of the program, it doesn't throw any error, which is fine.</p>
  
  <p>At the start of my program, I run this to check if we're in 64-bit and if so, go to a folder containing the 64-bit file:</p>
  
  <pre><code>if ((IntPtr.Size &gt; 4) &amp;&amp; ((ad.RelativeSearchPath == null) || !ad.RelativeSearchPath.Contains(ad.BaseDirectory + "64\\")))
          {
              AppDomainSetup aset = new AppDomainSetup();
              aset.PrivateBinPath = ad.BaseDirectory + "64\\;" + ad.BaseDirectory;
              aset.PrivateBinPathProbe = ad.BaseDirectory + "64\\;" + ad.BaseDirectory;
              ad = AppDomain.CreateDomain("_NM64SWITCH_", Assembly.GetAssembly(typeof(NaviModel.Program)).Evidence, aset);
              ad.ExecuteAssemblyByName(Assembly.GetAssembly(typeof(NaviModel.Program)).FullName, par);
              return;
          }
  </code></pre>
  
  <p>I'm a little lost as to where to look now.</p>