使用 cffi 从 python 调用 fortran dll



我目前正在研究一种模拟工具,该工具需要Fortran dll的PDE求解器。为了弄清楚如何从python调用dll,我使用了来自同一dll的更简单的函数,但无法使其工作。

系统规格: 视窗 7 专业版 (64 位( 斯皮德 3.2.8 蟒蛇 3.6.5(32 位(

我现在使用 cffi 来调用 fortran 函数,但它也不起作用。

    import cffi as cf
    ffi=cf.FFI()
    lib=ffi.dlopen("C:WindowsSysWOW64DLL20DDS")
    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

这是我用来用 cffi 调用函数的代码。我正在加载的 dll 包含我打算调用的函数 S01BAF。 我收到的错误消息是:

   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

我不知道这是什么意思。

为了检查函数本身是否正常工作,我尝试从其他语言(VBA(调用它,它工作得很好。

    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

消息框显示 ln(2( 的正确值。

我已经阅读了之前提出的问题,但无法将答案应用于我的问题。

这是多亏了@Joe才能工作的代码!

    ffi=cf.FFI()
    lib=ffi.dlopen("C:WindowsSysWOW64DLL20DDS")
    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))

干杯 蒂洛

S01BAF的函数定义

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

指示变量xifail是指针。请尝试

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

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

最新更新