Erroneous results in array returned from fortran subroutine

I call an array from a Fortran subroutine.
The subroutine runs as it should in a fortan program and gives correct results.
When I call the subroutine in VBA from the fortran dll, I get erroneous results.
No idea what goes wrong I suppose the error is on the VBA side because the program gives the correct results.
In the spreadsheet =Max_Min(L, P1, P2, P3, P4, a1, a2, a3, t)
gives the following result: 141.00 -120.00 2.00 5.6 6.00 7.8
Whereas running the progam gives the following correct results:
141.51 -120.27 9.90 9.90 6.00 7.80
This is very strange.
ar(1) and ar(2) seem to be transformed in integers
ar(3) and ar(4) are erroneous
ar(5) and ar(6) seem to be correct

Declare PtrSafe Sub MaxMin Lib "D:\fortran_projects\kraanbaan_excel\kraanbaan_dll.dll" (ByVal L As Double, _
ByVal P1 As Double, ByVal P2 As Double, ByVal P3 As Double, ByVal P4 As Double, ByVal a1 As Double, _
ByVal a2 As Double, ByVal a3 As Double, ByVal t As Double, ByRef ar As Double)

Function Max_Min(L As Double, P1 As Double, P2 As Double, P3 As Double, P4 As Double, a1 As Double, _
a2 As Double, a3 As Double, t As Double)

Dim ar(1 To 6) As Double

'Call Fortran
MaxMin L, P1, P2, P3, P4, a1, a2, a3, t, ar(1)
Max_Min = ar
End Function



I have never called Fortran from VBA, but have you got matching variable types? Are you using iso c bindings? (probably a good idea).

Matching variable types is OK.
VBA - Fortran is indeed very picky about that but when the variable types wouldn’t match I would get no result at all.
In the same Fortran DLL are also functions returning one variable and those run without a problem.
This one returns an array. The results are okay when I run in a Fortran program.
VBA must be doing something weird with this array but I have no idea what it is.
Roger

FYI, relevant discussion: Iso_c_binding: pass an array from c to fortran (Edit: python interop content) - #10 by eelis

FYI changing all real(kind=8) to real on the Fortran side and Double to Single on the VBA side did the trick.

Roger