0
$\begingroup$

Following this example, I have tried to call subroutines written in FORTRAN which uses a fixed length character like "Character*255" (here 255 is an example). If the character length is given as Dimension(8) (here 8 is an example), I can call the function from Mathematica via the .dll file with "String[]" as the input argument. But when calling the subroutine with "Character*255" using "String[]", it fails.

The following codes show both subroutines in FORTRAN and Mathematica codes calling them.

FORTRAN subroutines.

(* Subroutine using DIMENSION as array for character *)
SUBROUTINE charsubtest(x,z)   
IMPLICIT NONE

Character, DIMENSION(5),INTENT(in)::x
REAL*8, INTENT(out)::z

z=1542.

RETURN
END SUBROUTINE

(* Subroutine using Character* as array for character *)
SUBROUTINE charStarsubtest(x,z)
IMPLICIT NONE

Character*5, INTENT(in)::x
REAL*8, INTENT(out)::z

z=1542.

RETURN
END SUBROUTINE

Using gfortran, creates the dll file as follows.

gfortran -c testfuncsubroutine.f90
gfortran -shared -mrtd -o testfuncsubroutine.dll testfuncsubroutine.o

Now, call the function using "NETLink" from Mathematica as follows.

Needs["NETLink`"]; ReinstallNET["Force32Bit" -> True]; path3 = FileNameJoin[{Directory[], "testfuncsubroutine.dll"}]; TestCharInSubroutine = DefineDLLFunction["charsubtest_", path3,
"void", {"String[]", "Double*"}];

resCharfuncSub = MakeNETObject[{0}, "System.Double[]"]; TestCharInSubroutine[{"eight"}, resCharfuncSub]; NETObjectToExpression[resCharfuncSub]

{1542.}

Now call the second subroutine.

Needs["NETLink`"]; ReinstallNET["Force32Bit" -> True]; path3 =FileNameJoin[{Directory[], "testfuncsubroutine.dll"}]; TestCharInV1Subroutine = DefineDLLFunction["charStarsubtest", path3, "void", {"String[]", "Double*"}];

The output is:

NET::netexcptn: A .NET exception occurred: System.EntryPointNotFoundException: Unable to find an entry point named 'charStarsubtest' in DLL 'C:\Users\kyaw\Documents\Wolfram Mathematica\KT_test_2016\MathematicaTips\FortranDllTest\testfuncsubroutine.dll'. at Wolfram.NETLink.DynamicDLLNamespace.DLLWrapper1.charStarsubtest(String[] , Double* ).

In fortran, the length of the input text doesn't matter and gave the output. The following is the fortran main program that calls these subroutines.

Program main

IMPLICIT NONE

Character, DIMENSION(8)::s="FIVES128" Character*9::q="ELEPHANTS" REAL*8::z

Call charsubtest(s,z)

Print*, z

Call charStarsubtest(q,z)

Print*, z

End

The output is:

1542.000000000000000 1542.000000000000000

In fact, these subroutines use character*255 as input strings and I intend to return character*255 as well. Even input can't be done thus why I haven't tried the return.

Any help and advice are highly appreciated.

Yaykhel

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.