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

Fortran and C# interop - I/O

jcunsolo
Beginner
1,227 Views

Hi All,

Am trying to integrate a Fortran dll into a C# application with limited success.

I am using Intel Fortran Compiler 10.1.021 (IA-32) and a trial version of Visual Studio 2008.

I am able to pass parameters back and forth between the two projects and "step into" the Fortran dll when debugging.

However, when I try and do any I/O type operations in the Fortran dll a System.BadImageFormatException is thrown at the C# end. In the code snippets below uncommenting the "print *, num" cause the program to crash. doing any sort of IO will cause the program to crash with a System.BadImageFormatException error.

Here is a simplified version of the two code snippets:

C# Code:

// C# calling Fortran example
// Part of the Intel Visual Fortran sample programs
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
///


/// Summary description for Class1.
///

class Class1
{
///
/// The main entry point for the application.
///

[DllImport(@"visualfortranDll1.dll")]
public static extern void visualfortranDll1(
[MarshalAs(UnmanagedType.R4)]out System.Single num);
[STAThread]
static void Main(string[] args)
{
System.Single num = 0F;
for (int i = 0; i < 10; i++)
{
visualfortranDll1(out num);
Console.WriteLine(num);
Console.ReadLine();
}
}
}
}

Fortran Code:

C! visualfortranDll1.f90
C!
C! FUNCTIONS/SUBROUTINES exported from visualfortranDll1.dll:
C! visualfortranDll1 - subroutine
C!
subroutine visualfortranDll1(num)

C ! Expose subroutine visualfortranDll1 to users of this DLL
C !
!DEC$ ATTRIBUTES DLLEXPORT::visualfortranDll1
!DEC$ ATTRIBUTES ALIAS:'visualfortranDll1'::visualfortranDll1

C ! Variables
&nb sp; REAL, INTENT(OUT) :: num

C ! Body of visualfortranDll1
num = num + 10 ! num = MOD(r1, 256.0)
!This line will throw a System.BadImageFormatException
! print *, num

end subroutine visualfortranDll1

P.S. Yes, this is a blatant rip off of the visualFortranDll1 example.

Thanks for getting me this far!

-Joe

0 Kudos
14 Replies
Steven_L_Intel1
Employee
1,227 Views
You can't "print" from a DLL that is called from a non-console application, as there is no console. You could create one by calling AllocConsole (a Win32 API routine) first. I tend to prefer using MessageBox in such circumstances, but your taste may differ.
0 Kudos
jcunsolo
Beginner
1,227 Views

Steve,

Thanks for your prompt response, much appreciated, two things:

1. As stated previously "doing any sort of IO will cause the program to crash with a System.BadImageFormatException error", more importantly the C# throws a System.BadImageFormatExcpetion when doing somethings like:

open(1, file=filename)

close(1)

2. The C# actually is a console application, not sure what effect that has on things.

Cheers,

Joe

0 Kudos
Steven_L_Intel1
Employee
1,227 Views
Ok. Try changing the "Use Run-Time Libraries" settings under Fortran > Libraries in your DLL. I suggest starting with "Multithreaded DLL" and see if that helps.
0 Kudos
jcunsolo
Beginner
1,227 Views

Again thanks for the speed of the reply, but alas to no avail.

It was originally set to "Debug Multithread DLL" and I have changed it to "Multithread DLL", but nothing.

Is it conceivable that this is a "build platform" issue? I'm using 32-bit XP.

Joe

0 Kudos
Steven_L_Intel1
Employee
1,227 Views
See the attached project which works and compare it to yours.
0 Kudos
jcunsolo
Beginner
1,227 Views

Yes,

That is the same project that I have blatantly ripped off to illustrate my problem. Unfortunately, that project because of the calls to print (and even MOD) cause the BadImageFormatException. If those (IO) lines of code are removed the project runs as you would expect.

Thanks in advance,

Joe

0 Kudos
Steven_L_Intel1
Employee
1,227 Views
Well, I just built and ran it and it worked for me.

What's the value of the PATH environment variable on your system? I wonder if you have some 64-bit DLLs that are being found. Or, download DependencyWalker and use it to examine your DLL - does it report any seriosu problems?
0 Kudos
jcunsolo
Beginner
1,227 Views

So ran dependency walker and here is what it said:

1. Couldn't open MSJAVA.dll, although I'm not sure how important that is.

2. The output terminated with:

GetProcAddress(0x7C800000 [KERNEL32.DLL], "FlsAlloc") called from "MSCOREE.DLL" at address 0x790068DF and returned 0xFFBADD11.

Exited "CONSOLEAPPLICATION4.EXE" (process 0x450) with code -1073740791 (0xC0000409).

Couldthat imply that the FlsAlloc function could not be found in Kernel32.DLL? And then what does that mean?

A message board on Microsoft says, "It turns out that one of the library's I was statically linked to was using a different version of the CRT, so rebuilding it with the right version seems to have solved my problems". Do you think this could be related? And if so how do you rebuild with a different version?

Cheers,

Joe

0 Kudos
imeca
Beginner
1,227 Views
Hello;
steve, i downloaded the sample you attached. when i opened the "visualfortranDll1.sln", the visual studio 2005 that i have made a conversion of the project. this sample showed me somethings i didn't know before. so i tried to make a similar simple project to ensure that i understood the basic of things. unfortunately this didn't work.
when i focused, i saw that the fortran DLL part of the project generates tha DLL directly in the debug folder of the C# project part. this is automatically done every project generation. in the sample i created, the DLL is created in the folder of the fortran part of the project. i saw that a property called "output folder" is set to $(configurationname). i think that this is the property which manages all thing. so i wanted to ask you, is there any method to automate the generation of the DLL and its use from the C# project ? and is this all i have to know when i want to call fortran routines from a C# project ?

thanks steve for the help you're providing us with!
0 Kudos
Steven_L_Intel1
Employee
1,227 Views
What I prefer to do is add a post-build step for the DLL project to copy the DLL where it needs to go. Having two projets share an output directory causes problems on a rebuild.
0 Kudos
imeca
Beginner
1,227 Views
sorry but there is no post build event in the dll project of your sample.
and in such a case, what is the command line i have to add.
(i am sorry if my question seems eveident but i a m basically a mechanical engineer not a computer science engineer)
0 Kudos
Steven_L_Intel1
Employee
1,227 Views
I didn't create that sample, just adapted it.. Look at the DLLDynamicLoad sample to see a post-build event on the DLL project.
0 Kudos
imeca
Beginner
1,227 Views
excuse me but where can i find this sample?
0 Kudos
Steven_L_Intel1
Employee
1,227 Views
In the Samples subfolder under where Intel Visual Fortran 10.1 is installed.
0 Kudos
Reply