Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1 View

Not able to use 'GetProcAddress' from Fortran to call .Net methods

Hi All,

Please escuse me if this post seems silly. I'm totally new to Fortran and have been tasked to use some existing Fortran DLL's in my .NET project.

I'm able to access functions created in Fortran from .net code (C# to be precise) and get replies/values back.

I also have some functions in Fortran which call external methods directly(previously they were used to call Delphi methods).

Unfortunately the same does not work on C#. Do I have to change something on the C# end get it working.

I cannot change any code in fortran as its maintained by someone else and I know fortran part works as i'm able to see data in delphi.

Have any one encountered such issues ? if so it would be great if you can direct me to some URL or jolt down some steps as to how I can impliment this.

The following is my C# code (simplified) : 

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

namespace MYAPP
{
    public partial class Form1 : Form
    {

        private const string dll_Name = "FortranDLL.dll";
        static string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        byte[] fileName = System.Text.Encoding.ASCII.GetBytes(filePath);

        //This works fine
        [DllImport(dll_Name, CallingConvention = CallingConvention.Cdecl)]
        public static extern int INITIALISE(int callerId, byte[] fileName);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int callerId = 2;
            int returnValue = INITIALISE(callerId, fileName);
            Console.WriteLine("Initialize call " + returnValue.ToString());
            // The above statement works and i can get proper value
        }
        
        [ComVisible(true)]
        public static void SetMessageString(int one, int two, byte[] arr)
        {
            Console.WriteLine("Hello there ! ");
        }

    }
}

 Thanks & regards,

Rohan W.

0 Kudos
1 Reply
Highlighted
Valued Contributor III
1 View

First, I presume you know

First, I presume you know about managed vs unmanaged code on the Microsoft Windows platform; if not, see here https://social.msdn.microsoft.com/Forums/vstudio/en-US/a3e28547-4791-4394-b450-29c82cd70f70/managed-code-vs-unmanaged-code?forum=csharpgeneral

Re; Fortran and ,NET, here are some threads for additional background:

https://software.intel.com/en-us/forums/topic/515995

https://software.intel.com/en-us/forums/topic/391002

https://software.intel.com/en-us/forums/topic/509148

Lastly, you need to provide further information on what exactly you mean when you say, "Unfortunately the same does not work on C#".  If you define static methods, as described in the second and third Fortran threads above, they can be called from Fortran.

0 Kudos