title: Using a 32bit or 64bit dll in C# DllImport
body: <p>Here is the situation, I'm using a C based dll in my dot.net application. There are 2 dlls, one is 32bit called MyDll32.dll and the other is a 64bit version called MyDll64.dll.</p>
  
  <p>There is a static variable holding the DLL file name: string DLL_FILE_NAME.</p>
  
  <p>and it is used in the following way:</p>
  
  <pre><code>[DllImport(DLL_FILE_NAME, CallingConvention=CallingConvention.Cdecl, EntryPoint=Func1")]
  private static extern int is_Func1(int var1, int var2);
  </code></pre>
  
  <p>Simple so far.</p>
  
  <p>As you can imagine, the software is compiled with "Any CPU" turned on.</p>
  
  <p>I also have the following code to determine if the system should use the 64bit file or the 32bit file.</p>
  
  <pre><code>#if WIN64
          public const string DLL_FILE_NAME = "MyDll64.dll";
  #else
          public const string DLL_FILE_NAME = "MyDll32.dll";        
  #endif
  </code></pre>
  
  <p>By now you should see the problem.. DLL_FILE_NAME is defined in compilation time and not in execution time so the right dll isn't loaded according to the execution context.</p>
  
  <p>What would be the correct way to deal with this issue? I do not want two execution files (one for 32bit and the other for 64bit)? How can I set DLL_FILE_NAME <em>before</em> it is used in the DllImport statement?</p>