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

Error with the IFX compiler when I call a Fortran subroutine from C#

dj1967
Novice
282 Views

Hello,

I'm using Intel Fortran 2024.1 and Visual Studio 2022 Professional.

When I call a basic Fortran subroutine from C# I don't get any errors get with the IFORT compiler. However when I switch to the IFX compiler, I get the following error:

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at CallDLL_64.Program.MYSUB64(Int32& ic, Int32& idiag, String filename, Int32 L1)
at CallDLL_64.Program.Main(String[] args)

 

The Fortran code is:

MODULE DllInterface  
   IMPLICIT NONE 
   
CONTAINS
   
!DEC$ ATTRIBUTES DLLEXPORT::MYSUB64
!DEC$ ATTRIBUTES ALIAS : "MYSUB64" :: MYSUB64
!
     SUBROUTINE MYSUB64(ic, idiag, filename)
 
      IMPLICIT NONE
      
      INTEGER*4, INTENT(INOUT)     :: ic, idiag
      CHARACTER*(*)                :: filename 
!
!--------------------------- Body of program  --------------------------
!       
!
      ic = 1
      idiag = 2
      
      RETURN
     END  
   
END MODULE DllInterface 

 

The C# code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CallDLL_64
{
class Program
{
[DllImport(@"..\\..\\..\\bin\\x64\\myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void MYSUB64(ref int ic, ref int idiag, string filename, int L1);


static void Main(string[] args)
{

int ic;
int idiag;
string X = "Hello";
int L1;

try
{

ic = 0;
idiag = 0;
L1 = X.Length;
MYSUB64(ref ic, ref idiag, X, L1);

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

 

What am I doing wrong?  

Thanks

0 Kudos
5 Replies
Ron_Green
Moderator
273 Views

The ifort project is also x64 or is it ia32 project?

0 Kudos
dj1967
Novice
151 Views
0 Kudos
Ron_Green
Moderator
272 Views

Here is an interesting thread on compiling the C# for x64/ia32.  The BadFormatException occurs when there is a mismatch between the calling C# program and the DLL in x64 vs ia32

Ron_Green
Moderator
219 Views

I don't know much about C#, but I did build a DLL from your code.  Using a default DLL project configuration.  In the thread I mentioned, https://stackoverflow.com/questions/5229768/c-sharp-compiling-for-32-64-bit-or-for-any-cpu 

(i forgot to put this in the last reply).
from a web search I learned you can tell if a DLL is ia32 or x64 by bringing the binary DLL into Notepad or Notepad+ and looking for the string "PE. " followed by either a letter:

  • Determining DLL Architecture:
    You can determine if a DLL is 32-bit or 64-bit by opening it in a text editor (like Notepad++) and searching for the "PE" header. The character following "PE" indicates the architecture. For 32-bit DLLs, the character after "PE" will be "L", while for 64-bit DLLs, it will be "t". 
     
    The default for ifx is "t" which as expected is a x64 DLL.  So I think your C# code must be looking for a 32bit DLL
     
0 Kudos
dj1967
Novice
150 Views

Hello Ron

I've opened the IFX DLL with Notepad and the header says:

MZx   @ x º ´ Í!¸LÍ!This program cannot be run in DOS mode.$ PE d† .ùh ð

The IFORT DLL also says

PE d†

Is there anything wrong?

Thanks.

 

0 Kudos
Reply