Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

C# calling fortran function errors out with BadImageFormatException

Charles_G_2
Beginner
1,116 Views

I have been given a fortran dll created by a coworker to use in my c# app. the dll was compiled as 32 bit dll and the function i desire has been exported. i confirmed the export using DUMPBIN /EXPORTS my.dll and the function i want to use is listed.

When I try to call the function i get an exception: BadImageFormatException - an attempt was made to load a program with an incorrect format.

My application has been compiled with VS 2010 as 32 bit but I am running on Windows 7 64 bit. Below is my c# code:

using System;

namespace FortranTest
{
class Program
{
static void Main(string[] args)
{
double p = (double)2500.0;
double t = (double)950.0;
short b = (short)1;

float h = FortranWrapper.ENTPT(ref p, ref t, ref b);
}
}
}

using System;
using System.Runtime.InteropServices;

namespace FortranTest
{
class FortranWrapper
{
[DllImport("My.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern float ENTPT(ref double Pa, ref double Tx, ref short b);

}
}

I did NOT install the Fortran development environment but the runtime libs located here:

http://software.intel.com/en-us/articles/redistributable-libraries-for-the-intel-c-and-visual-fortran-composer-xe-for-windows.

I have used the DependencyWalker to verify I have the required files and it looks pretty good. I am only mission 2 files: GPSVC.DLL and IESHIMS.DLL
but these are missing for applications that work so i don't think it is a problem.

I know very little about fortran but need help getting this working. its probably something stupid that i am doing but i cant find it.

Sorry for posting again....The first post didn't show up so I am reposting.

Thanks in advance for any help!!

charlie

0 Kudos
6 Replies
mecej4
Honored Contributor III
1,116 Views

Provide the /platform:x86 option to the C# compiler.

Alternatively, have your coworker build you a 64-bit DLL.

0 Kudos
Charles_G_2
Beginner
1,116 Views

mecej4,

Thank you for your help.   I am targeting the x86 platform but still receiving the error message.

I have targeted the x86 framework by looking at the project build properties and setting Platform target to be x86.

If that is incorrect or if you have other ideas let me know?

Does the person creating the fortran dll have to do anything special while compiling?

Thanks,

Charlie

0 Kudos
Charles_G_2
Beginner
1,116 Views

mecej4,

Thanks for your help.

I verified the platform I am compiling against and it is x86 - at least according to the Project Properties Build page and the Platform Target says x86.  I still get the same message.

are there any options the fortran dll needs built with?

Thanks,

Charlie

0 Kudos
mecej4
Honored Contributor III
1,116 Views

Because of the plethora of options and settings available in the IDE, it is difficult to pinpoint why the build error occurred unless you provide the log of the build.

You may find it useful to look at a recent thread on a similar problem: http://software.intel.com/en-us/forums/topic/391002 . To build the example given there using 32-bit compilers on 64-bit Windows the commands are

[bash]

ifort /LD /Od cbk.f90 /FeCallbackTest.dll
csc cbk.cs /platform:x86

[/bash] 

You could build and run that example, and then attempt to duplicate the build using the VS IDE.

0 Kudos
Steven_L_Intel1
Employee
1,116 Views

No, the Fortran DLL doesn't need specific options.

0 Kudos
Charles_G_2
Beginner
1,116 Views

Well, I got it working but I am not able to determine the exact problem or what fixed it.

I found out that the data types in the FortranWrapper should have been Single instead of Double but I don't think that was the initial problem.

It started working when I clicked the "Allow UnSafe Code".  However, I then unchecked it and it still ran.

If I am able to narrow down the problem, i will post more info.

Thanks for your help.

0 Kudos
Reply