Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Java / Fortran JNA compatibility

Renaud_Egal
Novice
913 Views
Hi,
I would like to develop a GUI in Java, calling Fortran dll.
I succedded in calling the dll from java, but it seems that the values given to the dll are incorrect.
Fortran code
[fortran]function hello(texte, long) !DEC$ ATTRIBUTES C, ALIAS:'foohell', DLLEXPORT :: hello INTEGER, value :: long CHARACTER(LEN=long) ::texte integer :: hello PRINT*, texte END FUNCTION[/fortran] Java code
[java]import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.PointerType; import com.sun.jna.ptr.ByReference; import com.sun.jna.ptr.IntByReference; public class BeepExample { public interface F95Test extends com.sun.jna.Library { F95Test lib = (F95Test) Native.loadLibrary("C:/Users/egal/Documents/Visual Studio 2010/Projects/libF90/libF90/Debug/libF90.dll", F95Test.class); int foohell(String hw, int l); } public static void main(String[] args) { String hw = "Hello World"; int r = F95Test.lib.foohell(hw, hw.length()); } } [/java] The Netbeans console returns the 11 following characters:
So I guess that the passed argument "Hello World" is not taken into account.
Thanks in advance for your help
Ren
0 Kudos
2 Replies
Arjen_Markus
Honored Contributor II
913 Views
A variable of the type String is an object. You have to convert it to a plain string (sequence of bytes) first.
IIRC, you need to transform the string into an array of bytes.

Regards,

Arjen
0 Kudos
Renaud_Egal
Novice
914 Views
Hello Arjen,
thanks for answering on so short notice.
Turns out I found something more "direct" to pass a string from java to fortran.
[java]... void STRSUB(String daa, int len); ...[/java]
[java] //sub strsub String saa = "HelloWorld"; F95Test.lib.STRSUB(saa, saa.length()); System.out.print("n");[/java] [fortran]subroutine STRSUB(str, len) !DEC$ ATTRIBUTES DLLEXPORT::STRSUB INTEGER, value :: len CHARACTER(len) :: str PRINT*, "dll strsub : ", str end subroutine STRSUB[/fortran] Works better and it is simpler.
Actually, I think that the real problem was the fact I was not compiling my dll with proper options (calling convention).
Thanks anyway, problem solved ;)
0 Kudos
Reply