How to fix the compiler error CS0009 - An attempt was made to load a program with an incorrect format?

I'm trying to reference the Microsoft ActiveX Data Objects 6.1 Library (ADODB) using the csc.exe C# compiler but I'm getting the error:

fatal error CS0009: Metadata file 'c:\Program Files\Common Files\system\ado\msado15.dll' could not be opened -- 'An attempt was made to load a program with an incorrect format. '

I have simplified the files to try tracking the problem.

Batch file Compile.bat:

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"C:\Program Files\Common Files\system\ado\msado15.dll" Code.cs

C# file Code.cs:

class Program {
  static void Main() {
    ADODB.Connection ADODBConnection = new ADODB.Connection();
    System.Console.WriteLine((ADODBConnection == null).ToString());

I have tried copying the dll to the same folder and referencing it but got the same error.

I also have the compilers versions ...\v2.0.50727\csc.exe and ...\v3.5\csc.exe and the libraries versions msado20.tlb, msado21.tlb, msado25.tlb, msado26.tlb, msado27.tlb, msado28.tlb and msado60.tlb.

When I try the same on Visual Studio 2017 (.NET Framework 4 Console Application) it works. It creates ConsoleApp1\ConsoleApp1\obj\Debug\Interop.ADODB.dll and references that instead but I don't know where it gets it from. I have tried searching for it in different folders.

1 answer

  • answered 2019-12-05 16:44 user7393973

    I found the file that Visual Studio uses from this thread (by searching online for Interop.ADODB.dll file location) and later this link:

    C:\Program Files\Microsoft.NET\Primary Interop Assemblies\adodb.dll

    Working Compile.bat:

    %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"%ProgramFiles%\Microsoft.NET\Primary Interop Assemblies\adodb.dll" Code.cs