title: Inno Setup - External .NET DLL with dependencies
body: <p>I am trying to use a custom DLL in a Inno Setup script during installation. I wrote a very simple function that basically checks a connection string for a MySQL database using MySQL .NET connector (there is no MySQL client on the target server). The code of this exported function is:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>public class DbChecker
  {
      [DllExport("CheckConnexion", CallingConvention.StdCall)]
      public static int CheckConnexion([MarshalAs(UnmanagedType.LPStr)] string connexionString)
      {
          int success;
          try
          {
              MySqlConnection connection = new MySqlConnection(connexionString);
              connection.Open();
              connection.Close();
              success = 0;
          }
          catch (Exception)
          {
              success = 1;
          }
          return success;
      }
  }
  </code></pre>
  
  <p>The function is imported this way in Inno Setup :</p>
  
  <pre class="lang-none prettyprint-override"><code>[Files]
  Source: "..\..\MyDll\bin\x86\Release\*"; Flags: dontcopy;
  </code></pre>
  
  <p>and</p>
  
  <pre class="lang-pascal prettyprint-override"><code>[Code]
  function CheckConnexion(connexionString: AnsiString): Integer;
  external 'CheckConnexion@files:MyDll.dll,MySql.Data.dll stdcall setuponly loadwithalteredsearchpath';`
  </code></pre>
  
  <p>The problem is that the setup throws an exception at runtime:</p>
  
  <blockquote>
    <p>Runtime Error (at 53:207):  </p>
    
    <p>External exception E0434352.</p>
  </blockquote>
  
  <p>I think I have to use the <code>files</code> prefix because the function is called in the <code>NextButtonClick</code> event handler, before files are copied to the <code>{app}</code> directory.</p>
  
  <p>Both <code>MyDll.dll</code> and <code>MySql.Data.dll</code> are correctly extracted to the <code>{tmp}</code> directory at runtime.</p>
  
  <p>I tried both with and without the <code>loadwithalteredsearchpath</code> flag with the same result.</p>
  
  <p>What I found is that this error code is a generic .NET runtime error code. </p>
  
  <p>If I remove the part using <code>MySql.Data</code> it works perfectly fine (except that it does nothing...)</p>
  
  <p>As advised on other threads I've been trying to log the error in my .NET code using <code>EventLog</code> and <code>UnhandledException</code> but I have the same exception no matter what (and no log source is created), even without the MySQL part. I checked EventLog permissions on my computer.</p>
  
  <p>It seems that the exception is thrown as soon as I use anything else that "basic" C# code (whenever I try to load another DLL).</p>