使用ctypes和Python将字符串传递给Fortran DLL

5 投票
1 回答
2229 浏览
提问于 2025-04-18 06:27

我正在尝试在 Python 2.7 中使用 ctypes 加载一个 DLL。这个 DLL 是用 Fortran 写的,里面有多个子程序。我已经成功设置了几个导出的函数,这些函数的参数是 longdouble 指针。

import ctypes as C
import numpy as np

dll = C.windll.LoadLibrary('C:\\Temp\\program.dll')
_cp_from_t = getattr(dll, "CP_FROM_T")
_cp_from_t.restype = C.c_double
_cp_from_t.argtypes = [C.POINTER(C.c_longdouble),
                    np.ctypeslib.ndpointer(C.c_longdouble)]

# Mixture Rgas function
_mix_r = getattr(dll, "MIX_R")
_mix_r.restype = C.c_double
_mix_r.argtypes = [np.ctypeslib.ndpointer(dtype=C.c_longdouble)]

def cp_from_t(composition, temp):
    """ Calculates Cp in BTU/lb/R given a fuel composition and temperature.

    :param composition: numpy array containing fuel composition
    :param temp: temperature of fuel
    :return: Cp
    :rtype : float
    """
    return _cp_from_t(C.byref(C.c_double(temp)), composition)

def mix_r(composition):
    """Return the gas constant for a given composition.
    :rtype : float
    :param composition: numpy array containing fuel composition
    """
    return _mix_r(composition)

# At this point, I can just pass a numpy array as the composition and I can get the 
# calculated values without a problem
comps = np.array([0, 0, 12.0, 23.0, 33.0, 10, 5.0])
temp = 900.0

cp = cp_from_t(comps, temp)
rgas = mix_r(comps)

到目前为止,一切顺利。

问题出现在我尝试调用另一个叫 Function2 的子程序时,它需要一些字符串作为输入。这些字符串都是固定长度的(255),而且还要求每个字符串参数的长度。

这个函数在 Fortran 中的实现如下:

Subroutine FUNCTION2(localBasePath,localTempPath,InputFileName,Model,DataArray,ErrCode)
!DEC$ ATTRIBUTES STDCALL,REFERENCE, ALIAS:'FUNCTION2',DLLEXPORT :: FUNCTION2
Implicit None
Character *255 localBasePath,localTempPath,InputFileName
Integer   *4  Model(20), ErrCode(20)
Real      *8  DataArray(900)

在 Python 中,函数的原型设置如下:

function2 = getattr(dll, 'FUNCTION2')
function2.argtypes = [C.POINTER(C.c_char_p), C.c_long,
                      C.POINTER(C.c_char_p), C.c_long,
                      C.POINTER(C.c_char_p), C.c_long,
                      np.ctypeslib.ndpointer(C.c_long , flags='F_CONTIGUOUS'),
                      np.ctypeslib.ndpointer(C.c_double, flags='F_CONTIGUOUS'),
                      np.ctypeslib.ndpointer(C.c_long, flags='F_CONTIGUOUS')]

我用以下方式调用它:

base_path = "D:\\Users\\xxxxxxx\\Documents\\xxxxx\\".ljust(255)
temp_path = "D:\\Users\\xxxxxxx\\Documents\\xxxxx\\temp".ljust(255)
inp_file = "inp.txt".ljust(255)

function2(C.byref(C.c_char_p(base_path)),
                  C.c_long(len(base_path)),
                  C.byref(C.c_char_p(temp_dir)),
                  C.c_long(len(temp_dir))),
                  C.byref(C.c_char_p(inp_file)),
                  C.c_long(len(inp_file)),
                  model_array,
                  data_array,
                  error_array)

这些字符串实际上是路径。函数 Function2 无法识别这些路径,并且在最后给出了一个错误信息,里面有一些无法识别的字符,比如:

forrtl: severe (43): file name specification error, unit 16, D:\Users\xxxxxxx\Documents\xxxxx\ωa

我希望这个函数接收到的是 D:\Users\xxxxxxx\Documents\xxxxx\。显然,这些字符串没有正确传递。

我听说 Python 使用的是 NULL 结束的字符串。这在传递字符串给 Fortran DLL 时会是个问题吗?如果是的话,我该如何解决?

有什么建议吗?

1 个回答

3

根据@eryksun的评论,我做了一些修改让它能正常工作。

我把argtypes改成了:

function2 = getattr(dll, 'FUNCTION2')
function2.argtypes = [C.c_char_p, C.c_long,
                  C.c_char_p, C.c_long,
                  C.c_char_p, C.c_long,
                  np.ctypeslib.ndpointer(C.c_long , flags='F_CONTIGUOUS'),
                  np.ctypeslib.ndpointer(C.c_double, flags='F_CONTIGUOUS'),
                  np.ctypeslib.ndpointer(C.c_long, flags='F_CONTIGUOUS')]

而不是把字符串作为byref传递,我把它改成了下面这样。

base_path = "D:\\Users\\xxxxxxx\\Documents\\xxxxx\\".ljust(255)
temp_path = "D:\\Users\\xxxxxxx\\Documents\\xxxxx\\temp".ljust(255)
inp_file = "inp.txt".ljust(255)

function2(base_path, len(base_path), temp_dir, len(temp_dir), inp_file, len(inp_file), 
          model_array, data_array, error_array)

直接传递这些值就足够了。

撰写回答