Calling a fortran dll from phython using ctypes

0

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)

The pythoncode:

    import numpy as np
    import ctypes as ct

    libnag = ct.cdll.LoadLibrary("C:\Windows\SysWOW64\DLL20DDS")
    nag_ln = libnag.S01BAF
    nag_ln.restype=ct.c_double

    print (libnag)
    print (nag_ln)

    x = ct.c_double(1)
    ifail= ct.c_int(0)

    y=nag_ln(ct.byref(x),ct.byref(ifail))
    y=nag_ln(ct.pointer(x),ct.pointer(ifail))
    y=nag_ln(x,ifail)

Loading the DLL works as well as loading the function as can be seen by the print output:

    <CDLL 'C:\Windows\SysWOW64\DLL20DDS', handle bc80000 at 0x8af8c50>
    <_FuncPtr object at 0x07108378>

However calling the function doesn't work. I have tried the three listed possibilities obtaining different error messages. If I pass the variables byref or as a pointer i recieve this message:

    File "C:/Users/Student/Desktop/Minimal.py", line 21, in <module>
    y=nag_ln(ct.byref(x),ct.byref(ifail))

    ValueError: Procedure called with not enough arguments (8 bytes missing) or wrong calling convention

If I pass the variables by value I recieve this message:

    File "C:/Users/Student/Desktop/Minimal.py", line 23, in <module>
    y=nag_ln(x,ifail)

    OSError: exception: access violation reading 0x00000001

Regarding the used DLL: The dll contains various function and solvers. I am using a function that returns the value of ln(1+x). I can't provide the DLL itself but there is documentation available which should provide all necessary information: https://www.nag.co.uk/numeric/fl/nagdoc_fl26/html/s/s01baf.html

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 tried using pydll or windll instead of cdll but it made no differnce.
I have read the previously asked questions but couldn't apply the answers to my problem.

Any kind of help is appreachiated! Cheers, Thilo

python
python-3.x
dll
fortran
ctypes
asked on Stack Overflow Aug 9, 2018 by ThlHckmnn

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0