以Char为参数从Fortran DLL调用函数

2024-09-30 22:16:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python从第三方Fortran DLL调用函数(bubblesort)。我的问题是向函数传递一个字符。我使用cffi实现了如下所示的功能,但我想使用ctypes。
cffi版本:

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)

输出:

[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]

现在,我的ctypes版本:

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)

错误消息:

OSError: exception: access violation writing 0xFFFFFFFC

uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
cffi的一个问题是:当我再次调用该函数时,它不会对数组进行排序。我需要重新启动内核以获得正确的结果:

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)

输出:

[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]

uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
现在我正在Win10 64位上使用Spyder和Python 3.8.3 32位


Tags: importnpordercffiarrayintdoublect
1条回答
网友
1楼 · 发布于 2024-09-30 22:16:53

通过将字符长度作为正则整数(不是指针)传递到字符本身的指针之后,解决了所有问题。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)

相关问题 更多 >