Given a C library, foo.lib, and a C# console application, bar.exe, I'm trying to perform Platform Invoke. However I keep getting the following exception when invoking methods from the library
System.BadImageFormatException occurred HResult=-2147024885 Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
I have configured the compiler to build bar.exe as x64 and foo.lib is a x64 library. I've run the following commands to confirm this
>corflags bar.exe Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.0.30319.1 Copyright (c) Microsoft Corporation. All rights reserved. Version : v4.0.30319 CLR Header: 2.5 PE : PE32+ CorFlags : 1 ILONLY : 1 32BIT : 0 Signed : 0 >dumpbin /headers foo.lib ... File Type: LIBRARY FILE HEADER VALUES 8664 machine (x64) 3 number of sections 53B535D4 time date stamp Thu Jul 03 12:52:04 2014 10E file pointer to symbol table 8 number of symbols 0 size of optional header 0 characteristics ...
I'm certain that foo.lib is the library being loaded, as I tried deleting it, which results in a System.DllNotFoundException.
Any ideas of what might be wrong would be much appreciated.
Edit The exception occurs when I attempt to invoke the library the first time. I have a static class with the following declarations
private static class NativeMethods
{
private const string libname = "foo.lib";
[DllImport(NativeMethods.libname)]
public static extern void foo_method();
}
And the exception occurs in the first call to
NativeMethods.foo_method()