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

Call Fortran dll in C# Winform

jhlee0301
Beginner
1,424 Views

Hello guys.

I am trying to call Fortran dll from C# Winform.

This is very popular example which we can find in google.

https://stackoverflow.com/questions/10317691/making-fortran-dll-and-calling-it-from-c-sharp

I could success in C# console project but failed in C# Winform.

--- Fortran code ----------

FUNCTION TSAT11(P)
!DEC$ ATTRIBUTES ALIAS:'TSAT11' :: TSAT11
!DEC$ ATTRIBUTES DLLEXPORT :: TSAT11
!DEC$ ATTRIBUTES STDCALL :: TSAT11
!DEC$ ATTRIBUTES VALUE :: P
REAL, INTENT(IN) :: P
REAL :: TSAT11
! Examle calculation
TSAT11 = P - 273.15
RETURN
END FUNCTION

--- C# Console project (This is success) -----------

using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
class Program
{
[DllImport(@"D:\Temp\Dll_test1.dll")]
static extern float TSAT11(float P);

static void Main(string[] args)
{
Console.WriteLine("Hello World!");
float p = 300f;
float t = TSAT11(p);
// returns 26.8500061
Console.WriteLine(t.ToString());
}
}
}

--- C# Winform project -------------

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

 //[DllImport(@"D:\Temp\Dll_test1.dll", CallingConvention = CallingConvention.StdCall)]
[DllImport(@"D:\Temp\Dll_test1.dll")]
static extern float TSAT11(float P);

private void btnDoIt_Click(object sender, EventArgs e)
{
float p = 300f;
float t = TSAT11(p);         //Error still occurs here
MessageBox.Show(t.ToString());
}
}
}

 

Still I cannot find difference between console project and WinForm project...

 

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,409 Views

You will need to tell us what exactly goes wrong. Screenshots of error messages, etc. It would also be better if you attached a ZIP file of a VS solution with the two projects. I suspect there is not a lot of C# expertise in this forum.

View solution in original post

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
1,410 Views

You will need to tell us what exactly goes wrong. Screenshots of error messages, etc. It would also be better if you attached a ZIP file of a VS solution with the two projects. I suspect there is not a lot of C# expertise in this forum.

0 Kudos
jhlee0301
Beginner
1,384 Views

I found solution.

As you said there is one more thing in VS project file.

In build tab there is "prefer 32 bit" checked box and I unchecked it.

Now it works. Thank you.

 

0 Kudos
Reply