- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The ifort project is also x64 or is it ia32 project?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page