Yes, I have set up the reference and can call normal fortran functions from c# (for instance, anything I have to pass reals, integers, strings, arrays etc.) but I am not sure how to pass references to other functions. I did look in the sample and could not see any examples of this (only passing doubles, strings etc.) but maybe I am missing something.

To be clearer, I want to be able to define a function (in c#) and I want to be able to pass a reference to this function to a second function that I am calling from the fortran dll. The fortran function then calls the referenced c# function to execute certain parts of its code.

! A module to contain the user supplied delegate of the correct type.
! There needs to be one of these modules for each defined delegate.
module delegate_a_data
object("DelegateA") delegate_object
end module

! An interface to the users delegate for the library to call.
! There needs to be one of these for each defined delegate.
subroutine invoke_delegate_a (i)
use delegate_a_data
! Argument declarations.
integer i
! Invoke the user defined delegate.
assembly_external("DelegateA.Invoke") DelegateA_Invoke
print *, "in interface, invoking delegate"
call DelegateA_Invoke(delegate_object, i)
end subroutine

! An interface to the library routine which replaces all delegate arguments with externals.
! There needs to be one of these for each routine in the library that has an external argument.
subroutine itest_ (delegate_obj)
use delegate_a_data
assembly_interface(name="itest")
object("DelegateA") delegate_obj
external invoke_delegate_a
! Save the delegate object.
delegate_object = delegate_obj
! Call the library routine, passing on all arguments, but with all delegates replaced with their equivalent externals.
print *, "in interface, calling library"
call test(invoke_delegate_a)
end subroutine
[/pre]
===

Place all these files into the same directory and run build.bat from the same directory. It should produce 3 dll's and 1 exe.

Some notes:

This uses a .NET delegate as a callback routine (effectively a function pointer). The code is in VB.NET but should be fairly easy to port to C#. This code is designed to allow a VB.NET program call a routine in an FTN95.NET dll with minimal changes - so it uses the code in delegate_interface.f90 to make the call as transparent as possible.

The user's code is contained in delegate_main.vb, the delegate definitions are in delegate_defs.vb, the interface code is in delegate_interface.f90 and the library code is in delegate_library.f90.

The interface code is not re-entrant (so it's not thread safe).

The interface code (delegate_interface.f90) could be compiled into the same dll as the library code (delegate_library.f90), but the delegate definitions (delegate_defs.vb) need to remain separate (as they are referenced by both the interface code and the user's program).

More information:

All the delegate types need to be defined with the correct routine signatures so both the user's code and the library code can use them.