PDA

View Full Version : function pointers in Fortran95?


rosedd
06-10-2004, 04:21 PM
I have a Fortran95 dll that gets called from a C# application. I would like to be able to pass a function pointer (delegate in .NET) to a Fortran function so that the function can then call back into the C# code.

Is this possible? If so, how?

Thanks.

David Rose

tzeis
06-10-2004, 05:41 PM
This would be very difficult to do. I can see at least two brick walls: first, getting C# to pass the function pointer to the DLL; second getting the unmanaged DLL to call back into managed code. The only thing that pops into my mind as a way to do this is to write the delegate in C#, have the delegate call the Fortran DLL, and in the DLL, set a status variable that tells the C# delegate whether you want it to proceed normally, or to do the callback. You would return to the delegate, and have the delegate make the actual calls.

gentelam
06-24-2004, 08:24 PM
Hi,
I thought this is interesting topic. I don't know much of c#. But if you want I have an example using palin C.

Main and DLL is in Fortran. and there is C layer in between on either side to communicate between application and dll.


So it's like :

Application To DLL
Fortran => C To C => Fortran

You can easily replace Fortran with only C if you want on either side.

Madhav.

GDK_SWRI2
07-17-2007, 06:05 PM
I have recently encountered the need for managed C# application to pass a C# method delegate to LF managed Fortran library. The LF help topic 'DELEGATE Attribute' is to say the very least, not overly helpful. Here are snippets of files I have:

Class1.f95

MODULE ES

USE System

TYPE, DELEGATE(MemoryUpdate) :: memoryUpdate
END TYPE

!TYPE(EvntHndlr),PUBLIC,POINTER :: abstract_EventHandler1

ABSRACT INTERFACE
SUBROUTINE MemoryUpdate(ADDRESS,THEVALUE)
TYPE(System%Int16) :: ADDRESS
TYPE(System%Int16) :: THEVALUE
END SUBROUTINE
END INTERFACE

TYPE, NAMESPACE(DELEGATETEST) :: CLASS1
CONTAINS
PROCEDURE,NOPASS :: METHOD1
END TYPE CLASS1

CONTAINS
SUBROUTINE METHOD1(ARG)
TYPE(EvntHndlr),POINTER :: ARG

!abstract_EventHandler1 => ARG
END SUBROUTINE METHOD1
END MODULE ES


Memory.cs

namespace DelegateTest
{
class Memory
{
public void MemoryUpdate(int Address, int Value)
{
MessageBox.Show("MemoryUpdate method!");
}
}
}


Form1.cs

using ES;

namespace DelegateTest
{
delegate void MemoryUpdate(int Address, int Value);

public partial class Form1 :: Form
{
Memory memory;

public Form1()
{
InitializeComponent();
}

private void btnDelegateTest_Click(object sender, EventArgs e)
{
memory = new Memory();
MemoryUpdate memup = new MemoryUpdate(memory.MemoryUpdate);
memup(5,10); // No problems with delegate here

// How to set the delegate in LF Fortran?
//ES.EvntHndlr = memup;
//ES.EvntHndlr = new EvntHndlr(memory.MemoryUpdate);
//ES.DELEGATETEST.CLASS1.METHOD1(memup);
}
}

}

tzeis
07-18-2007, 06:22 PM
There is an example called delegate-event.f95 in the "dotNet Language Samples" folder. It shows the details of assigning a delegate using Fortran for .Net. In your case, I would first try doing all of the implementation of the delegate in C#, and in Fortran, instantiate the delegate class and add it to the event source as shown in the main program part of the example.