I have a very similar problem to the one described here: Make external functions accessible for other functions/modules in Fortran
I'm working with a DLL compiled from Fortran source code, using the Lahey/Fujitsu LF95 compiler and I'm trying to store a global reference to an external callback function (function pointer) so that it can be called at a later time from other functions in the Fortran DLL.
The scenario is something like this:
- the host application invokes a subroutine (procedure) from the Fortran DLL and passes a reference to the callback function
- a reference to the callback function should be stored as a global
- later, the host application may call various functions from the Fortran DLL and these functions need to invoke the callback function, calling into the host application.
The problem is that the accepted answer from the question doesn't compile with the Lahey Fortran compiler. Apparently, there are quite some differences between the Intel compiler and the LF95 compiler.
I did get the callback reference to work fine in a single subroutine, like this:
subroutine testcallback(cbk)
dll_export :: testcallback ! Lahey LF95 extern declaration to export this function
character(len=*) :: text
interface
subroutine cbk (string, length)
character(len=*), intent (in) :: string
integer, intent (in) :: length
end subroutine cbk
end interface
text = "Hello World"
call cbk(text, len(text)) ! invoke the cbk callback; works very well
return
end
Invoking this function from the host application (C# in my case but that's inconsequential) works very well. I can pass in a function reference (delegatae in C#) and Fortran makes the call correctly and I get the expected results.
The issue is that I can't seem to be able to move the interface
declaration outside of testcallback
, and then subsequently call the cbk
from a different Fortran function.
Here's an example of what I would want to accomplish:
subroutine setcallback(cbk)
dll_export :: setcallback ! Lahey LF95 extern declaration to export this function
interface
subroutine cbk (string, length)
character(len=*), intent (in) :: string
integer, intent (in) :: length
end subroutine cbk
end interface
! instead of calling cbk here, I'd like to save a reference to it and make it
! available to other functions..
return
end
subroutine testcallback()
dll_export :: testcallback ! Lahey LF95 extern declaration to export this function
character(len=*) :: text
text = "Hello World Again"
! somehow I want to be able to invoke the stored reference to cbk here
!
call cbk(text, len(text)) ! this obviously doesn't work like this
return
end
In closing I'd like to add that moving away from the LF95 compiler is not an option at this time. If anybody knows how to handle this I would very much appreciate it!