Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

A simple Fortran call from c#

mch7918
Beginner
1,570 Views
Hi,

I have created a simple Visula Studio project with a simple call to a Fortran routine. It builds with no errors,but when i run the solution i get the following error.

An unhandled exception of type 'System.BadImageFormatException' occurred in FortranTest1.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)


C# code
[DllImport("C:\\\\Users\\\\cmural1\\\\Desktop\\\\FortranTestSol\\\\FortranDLL\\\\Debug\\\\FortranDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void FortranDLL(
[MarshalAs(UnmanagedType.I4)] int int1,
[MarshalAs(UnmanagedType.I4)] int int2,
[MarshalAs(UnmanagedType.I4)] int int3);
private void button1_Click(object sender, EventArgs e)
{
int int1 = 1, int2 = 2, int3 = 3;
//char[] charVar = { 'a', 'b' };
//Form1.TestFort(ref boolVar, ref intVar, charVar);
Form1.FortranDLL(int1, int2, int3);
}

Fortran:
SUBROUTINE FortranDLL (int1, int2, int3)
!MS$ATTRIBUTES DLLEXPORT,ALIAS:'FortranDLL' :: FortranDLL

INTEGER :: int1,int2,int3

int2=int1+int3

RETURN
END

please find the attachment of the project.

0 Kudos
1 Solution
IanH
Honored Contributor III
1,570 Views
I think you have an argument passing mismatch. The C# code is passing by value (no ref or out keywords before the parameters in the declaration), the Fortran code expects passing by reference (no VALUE attribute). As the Fortran is modifying one of its arguments you probably want to add the ref and out keywords to the C# where appropriate.

I'd strongly recommend a BIND suffix on the Fortran subroutine statement. You can specify the "alias" for the procedure in the NAME clause.

View solution in original post

0 Kudos
7 Replies
mch7918
Beginner
1,570 Views
I even build the dll as 64-bit as i am running it on 64-bit machine.

Now i get following error.

An unhandled exception of type 'System.AccessViolationException' occurred in FortranTest1.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

any help would be greatly appreciated.
0 Kudos
SergeyKostrov
Valued Contributor II
1,570 Views
Please verify that:
a 64-bitapplication loads / uses a 64-bit Fortran DLL
or
a 32-bitapplication loads / uses a 32-bit Fortran DLL
Also, could you attach a test project in a zip-archive instead of arar-archive? I couldn't extract that content.
0 Kudos
mch7918
Beginner
1,570 Views
Hi,

Please find the attachment.
0 Kudos
SergeyKostrov
Valued Contributor II
1,570 Views
Please take a look at a'Form1.cs' source file and let's assume that a Win32 configuration is selected. Then,
a 64-bit FortranDLL will be imported andloaded at runtimeandit can be the main source of your problems:

...
[DllImport("C:\\Users\\cmural1\\Desktop\\FortranTestSol\\FortranDLL\\x64\\Debug\\FortranDLL.dll", CallingConvention = CallingConvention.Cdecl)]
...

In order to avoid any confusions with DLLs compiled for different platforms some suffixes could be added, like:

for a 32-bit platform: FortranDLL32.dll

and

for a 64-bit platform: FortranDLL64.dll
0 Kudos
mch7918
Beginner
1,570 Views
I ensured that the solution picks up the correct dll according to the configuration. It gives me the same error again and again. I tried to create new solutions but no use.

An unhandled exception of type 'System.AccessViolationException' occurred in FortranTest1.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
0 Kudos
IanH
Honored Contributor III
1,571 Views
I think you have an argument passing mismatch. The C# code is passing by value (no ref or out keywords before the parameters in the declaration), the Fortran code expects passing by reference (no VALUE attribute). As the Fortran is modifying one of its arguments you probably want to add the ref and out keywords to the C# where appropriate.

I'd strongly recommend a BIND suffix on the Fortran subroutine statement. You can specify the "alias" for the procedure in the NAME clause.
0 Kudos
mch7918
Beginner
1,570 Views
Thanks!! I was missing ref keywork before each parameter. I believed MarshallAs doesn't need such declarations.

-Chouhan
0 Kudos
Reply