title: Error loading DLL - Console App - Visual Studio Community 2022
body: <p>I'm trying to load a third-party DLL in a Console App project created in Visual Studio Community 2022. Environment is Windows 10. I have tried both .NET 5.0 and 6.0</p>
  <p>No matter what I have tried, I get the error &quot;Unhandled exception. System.BadImageFormatException: Bad IL format. The format of the file  is invalid.&quot;</p>
  <p>I have checked and normally this error is because I'm trying to load a x64 DLL in a x86 app or viceversa, but I know this is not the case because:</p>
  <ul>
  <li>I have been able to load this specific DLL in C (using LoadLibrary() in Codeblocks) and JAVA (using JNA), configuring the applications for a x86 target platform. I have configured this console app for a x86 target platform as well in properties.</li>
  <li>The DLL when loaded starts writing to a log file. The log file destination is fixed, and can't be deactivated. Whenever I run my app, I see the log being written to. I realized that the log was being written to by using Sysinternals' Process Monitor.</li>
  </ul>
  <p>The log only says</p>
  <blockquote>
  <p>INFO:   2.23.00 12:14:27(00000015)       PCLService.cpp:138    [22.11.2021]    logCreate</p>
  </blockquote>
  <blockquote>
  <p>INFO:   2.23.00 12:14:27(00000000)       PCLService.cpp:141    PDA_Manager: DllMain attached</p>
  </blockquote>
  <blockquote>
  <p>INFO:   2.23.00 12:14:27(00000000)       PCLService.cpp:145    PDA_Manager: DllMain detach</p>
  </blockquote>
  <p>Normally, the detach should only happen when the DLL is unloaded/program is closed. Here it happens automatically. It is like the program can't keep the DLL loaded, giving the error message.</p>
  <p>Here is my current code.</p>
  <pre><code>using System;
  using System.Reflection;
  
  namespace ConsoleApp5
  {
      internal class Program
      {
          static void Main(string[] args)
          {
              //var DLL = Assembly.LoadFile(@&quot;C:\Documentacion\VisaNet\Proyectos\Simpler\bin\Debug\x86\PCLService.dll&quot;);
              var DLL = Assembly.LoadFrom(&quot;PCLService.dll&quot;);
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  <p>I have tried both LoadFrom() and LoadFile(). Even tried Load(), but the error is different about additional information that I don't know how to get.</p>
  <p>I have checked Assembly Binding Log Viewer (fuslogvw.exe) and Process Monitor. I do not see any obvious problem.</p>
  <p><a href="https://i.stack.imgur.com/Ss8EA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ss8EA.png" alt="Process Monitor screenshot" /></a></p>
  <p>I assume that whatever error happens, it did happen by the time of the last 4 entries about PCLService.log, which must the ones writing in the DLL log about detaching, because there are no more entries about the log in Process Monitor.</p>
  <p>While I'm a experienced programmer, I'm totally new to Visual Studio / C#</p>
  <p>Any assistance would be welcome.</p>