pythonpython-3.xdllfortrancffi

Calling a fortran dll from python using cffi


I am currently working on a simultion-tool that requires a PDE Solver from a Fortran dll. In order to figure how calling a dll from python I used a simpler function from the same dll but can't get it to work.

Systemspecs: Windows 7 Professional (64bit) Spyder 3.2.8 Python 3.6.5 (32bit)

I am now using cffi to call the fortran function but it doesn't work either.

    import cffi as cf

    ffi=cf.FFI()

    lib=ffi.dlopen("C:\Windows\SysWOW64\DLL20DDS")

    ffi.cdef("""
             double S01BAF(double X, int IFAIL);
    """)

    print (lib)   #This works
    print (lib.S01BAF)   #This works

    x = 1.
    ifail = 0

    print (lib.S01BAF(x,ifail)) #This doesn't work

This is the code i am using to call the function with cffi. The dll i am loading contains the function S01BAF which i intend to call. The error message I recieve is:

   runfile('C:/Users/Student/Desktop/Minimal.py', wdir='C:/Users/Student/Desktop')
   <cffi.api._make_ffi_library.<locals>.FFILibrary object at 0x0759DB30>
   <cdata 'double(*)(double, int)' 0x105BBE30>
   Kernel died, restarting

I don't know what that means.

To check if the function itself is working I tryed calling it from a different language (VBA) and it worked just fine.

    Option Base 1
    Option Explicit

    Private Declare Function S01BAF Lib "DLL20DDS.dll" (x As Double, iFail As Long) As Double

    Sub ln()
        Dim iFail As Long
        Dim x As Double
        Dim y As Double

        x = 1
        iFail = 0
        y = S01BAF(x, iFail)
        MsgBox y
    End Sub

The messagebox displays the correct value for ln(2).

I have read the previously asked questions but couldn't apply the answers to my problem.

Here is the code that works thanks to @Joe!

    ffi=cf.FFI()
    lib=ffi.dlopen("C:\Windows\SysWOW64\DLL20DDS")
    ffi.cdef("double S01BAF(double *x, int *ifail);")

    x_1 = np.arange(-0.99,1,0.001)

    x = ffi.new('double*', 1)
    ifail = ffi.new('int*', 0)    

    y = (lib.S01BAF(x,ifail))

Cheers, Thilo


Solution

  • The function definition of S01BAF

    double s01baf_ (const double *x, Integer *ifail)
    

    indicates that the variables x and ifail are pointers. Please try

    x = cf.new('double*', 1.0)
    ifail = cf.new("int*", 0)    
    lib.S01BAF(x, ifail)
    

    or

    x = cf.new('double*')
    x[0] = 1.0
    ifail = cf.new("int*")    
    ifail[0] = 0
    lib.S01BAF(x, ifail)