Community
cancel
Showing results for 
Search instead for 
Did you mean: 
jhlee0301
Beginner
357 Views

Call Fortran dll in C# Winform

Jump to solution

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
Black Belt Retired Employee
342 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

2 Replies
Steve_Lionel
Black Belt Retired Employee
343 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

jhlee0301
Beginner
316 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.

 

Reply