Search code examples
pythondllfortranctypescffi

Calling function from Fortran DLL with Char as parameter


I'm trying to call a function (bubblesort) from a 3rd party Fortran DLL with Python. My problem is passing a char to the function. I got it working with cffi as shown below, but I want to use ctypes.
The cffi version:

import numpy as np
import cffi as cf

ffi=cf.FFI()
lib=ffi.dlopen(r"C:\Windows\SysWOW64\DLL20DDS")
ffi.cdef("""void M01CAF( double rv[], const int *m1, const int *m2, 
        const wchar_t *order, int *ifail);""")

m1 = 1
m1 = ffi.new("int*", m1)
m2 = 16
m2 = ffi.new("int*", m2)
order = ffi.new('wchar_t *', "A")
rvx = np.array([1.3, 5.9, 4.1, 2.3, 0.5, 5.8, 1.3, 6.5, 
                2.3, 0.5, 6.5, 9.9, 2.1, 1.1, 1.2, 8.6], dtype=float, order='F')
rv = ffi.cast("double* ", rvx.__array_interface__['data'][0])
ifail = 0
ifail = ffi.new('int*', ifail)

lib.M01CAF(rv, m1, m2, order, ifail)
print(rvx)

Output:

[0.5 0.5 1.1 1.2 1.3 1.3 2.1 2.3 2.3 4.1 5.8 5.9 6.5 6.5 8.6 9.9]

Now my ctypes version:

import numpy as np
import ctypes as ct
flib = ct.WinDLL('C:\Windows\SysWOW64\DLL20DDS.dll')
func = getattr(flib, "M01CAF")
func.restype = None

m1 = (ct.c_int32)(1)
m2 = (ct.c_int32)(16)
ifail = ct.c_int32(0)
rv = np.array([1.3, 5.9, 4.1, 2.3, 0.5, 5.8, 1.3, 6.5, 
                2.3, 0.5, 6.5, 9.9, 2.1, 1.1, 1.2, 8.6], dtype=ct.c_double, order='F')

order = 'A'
order = ct.c_wchar_p(order)

func.argtypes = (np.ctypeslib.ndpointer(dtype=ct.c_double, shape=(m2.value,)), 
                ct.POINTER(ct.c_int32), ct.POINTER(ct.c_int32), 
                ct.c_wchar_p, ct.POINTER(ct.c_int32))

func(rv, m1, m2, order, ifail)

Error Message:

OSError: exception: access violation writing 0xFFFFFFFC

______________________________________________________
A problem I have with cffi is: When I call the function again it does not sort the array. I need to restart my kernel in order to get the right result:

rvx = np.array([1.3, 5.9, 4.1, 2.3, 0.5, 5.8, 1.3, 6.5, 
                2.3, 0.5, 6.5, 9.9, 2.1, 1.1, 1.2, 8.6], dtype=float, order='F')
rv = ffi.cast("double* ", rvx.__array_interface__['data'][0])
lib.M01CAF(rv, m1, m2, order, ifail)
print(rvx)

Output:

[1.3 5.9 4.1 2.3 0.5 5.8 1.3 6.5 2.3 0.5 6.5 9.9 2.1 1.1 1.2 8.6]

______________________________________________________
Right now I'm using Spyder with Python 3.8.3 32bit on Win10 64bit.


Solution

  • Solved every problem by passing the length of the char as a regular integer (not pointer) after the pointer of the char itself. The same is necessary for CFFI.

    ## CTYPES
    import ctypes as ct
    flib = ct.WinDLL('C:\Windows\SysWOW64\DLL20DDS.dll')
    func = getattr(flib, "M01CAF")
    func.restype = None
    
    m1 = (ct.c_int32)(1)
    m2 = (ct.c_int32)(16)
    ifail = ct.c_int32(0)
    rv = np.array([1.3, 5.9, 4.1, 2.3, 0.5, 5.8, 1.3, 6.5, 
                    2.3, 0.5, 6.5, 9.9, 2.1, 1.1, 1.2, 8.6], dtype=ct.c_double, order='F')
    
    order = ct.c_wchar_p('A')
    func.argtypes = (np.ctypeslib.ndpointer(dtype=ct.c_double, shape=(m2.value,)), 
                    ct.POINTER(ct.c_int32), ct.POINTER(ct.c_int32), 
                    ct.c_wchar_p, ct.c_int32, ct.POINTER(ct.c_int32))
    
    print("BEFORE:")
    print(rv)
    
    func(rv, m1, m2, order, 1, ifail)
    
    print("AFTER:")
    print(rv)