forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing pointers to functions from c# to fortran

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Anonymous
Guest





PostPosted: Mon Jul 17, 2006 9:48 pm    Post subject: passing pointers to functions from c# to fortran Reply with quote

I have the following function in a fortran library that I want to call from C# under .NET:

in fortran .dll called 'odepack':

SUBROUTINE DLSODAR (F, NEQ, Y, T, TOUT, ITOL, RTOL, ATOL, ITASK,
1 ISTATE, IOPT, RWORK, LRW, IWORK, LIW, JAC, JT,
2 G, NG, JROOT)
assembly_interface (name="DLSODAR")
EXTERNAL F, JAC, G
...

In the C# code, I want to call the above function, and pass references to the three required external functions:

odepack.DLSODAR (F, NEQ, Y, T, TOUT, ITOL, RTOL, ATOL, ITASK,
ISTATE, IOPT, RWORK, LRW, IWORK, LIW, JAC, JT,
G, NG, JROOT);

The question is, how do I define the external functions in C# and how do I pass them via the interface? I am not overly experienced in either language.
Any help would be greatly appreciated.


Chris Blanksby
Back to top
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 5106
Location: Salford, UK

PostPosted: Tue Jul 18, 2006 6:36 am    Post subject: passing pointers to functions from c# to fortran Reply with quote

Chris

Have you looked at the Samples in the FTN95 help file and in C:Program FilesSalford SoftwareFTN95DemoNET FortranCalculatorCS etc.?

As I recall, all you need to do is to set up a reference to the DLL in your C# project.
Back to top
View user's profile Send private message
Anonymous
Guest





PostPosted: Tue Jul 18, 2006 4:30 pm    Post subject: passing pointers to functions from c# to fortran Reply with quote

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.
Back to top
Martin



Joined: 09 Sep 2004
Posts: 43

PostPosted: Wed Jul 26, 2006 3:56 pm    Post subject: passing pointers to functions from c# to fortran Reply with quote

I'm not sure this is exactly what you want, but it might help. The following example is a VB.NET program calling a routine in an FTN95.NET library, passing a reference to another VB.NET function.


=== File: delegate_defs.vb ===
[pre]
public delegate sub DelegateA (i as integer)
[/pre]
===

=== File: delegate_library.f90 ===
[pre]
subroutine test (callback)
external callback
integer i

print *, "in library, calling callback"

do i = 1, 3
call callback(i)
end do
end
[/pre]
===

=== File: delegate_interface.f90 ===
[pre]
! Interface between user's program and library dll.
! This references the library dll and the delegate definitions dll.


! 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]
===

=== File: delegate_main.vb ===
[pre]
' The user's program.
' This will reference the library interface dll and the delegate definitions dll.
module test

private sub MyCallback (i as integer)
System.Console.WriteLine("in callback: "+CStr(i))
end sub

sub main ()
System.Console.WriteLine("in main program, calling library via interface")
delegate_interface.itest(AddressOf MyCallback)
end sub

end module
[/pre]
===

=== File: build.bat ===
[pre]
ftn95 delegate_library.f90 /clr /link delegate_library.dll
vbc /target:library delegate_defs.vb
ftn95 delegate_interface.f90 /clr /ref delegate_defs.dll /ref delegate_library.dll /link delegate_interface.dll
vbc delegate_main.vb /r:delegate_defs.dll,delegate_interface.dll
[/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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group