我试图在python中调用fortran dll,但是遇到OSerror:异常:访问冲突读取0x00000000。
.dll函数调用应具有以下属性:
double FDIR(double *x,const double *a, int32_t *na, int32_t *k, double *h, double *x1,const int32_t *relparam, int32_t *m, int32_t *profile, int32_t *nlines, int32_t *nrefer, double *deltaX)
。另外,我有一个成功调用.dll的labview脚本,所以我知道问题出在python脚本中。
以下是我到目前为止的代码
from ctypes import *
import numpy as np
#Define Variables
x = np.arange(0, 5.52602+0.197358, 0.197358)
a = [2.65477,0.859621,0.0177231,0.0001,0,4.15638,0,0,
2.75715,-0.00442,
0,1,0,
0,1,0]
na = len(a)
k = 0
h = 0
x1 = 0
relparams = [0,0,0,0,0,0,0,0]
m = 8
profile = 1
nlines = 1
nref = 0
deltax = 0
#Conversions
a = np.asarray(a)
relparams = np.asarray(relparams, dtype = np.int32)
#Call Dll
fit_dll = cdll.LoadLibrary("mydll.dll")
#Loop dll
for x_ in x:
fit_dll.FDIR(c_double(x_),
a.ctypes.data_as(POINTER(c_double)),
c_int32(na),
c_int32(k),
c_double(h),
c_double(x1),
relparams.ctypes.data_as(POINTER(c_int)),
c_int32(m),
c_int32(profile),
c_int32(nlines),
c_int32(nref),
c_double(deltax))
print (result)
这里有什么东西可以作为一个明显的问题跳出来吗?非常感谢任何帮助/建议!
更新:感谢您的帮助,abarnert!我在argtypes和restype中添加了该函数。并在指针中添加了其余的变量。现在代替OSerror:exception:访问冲突读取0x00000000。我得到ValueError:调用过程没有足够的参数(缺少48个字节)或错误的调用约定。有什么想法吗?
from ctypes import *
import numpy as np
#Define Variables
x = np.arange(0, 5.52602+0.197358, 0.197358)
a = [2.65477,0.859621,0.0177231,0.0001,0,4.15638,0.0001,0.0001,
2.75715,-0.00442, # linear baseline
0,1,0, # etalon 1
0,1,0] # etalon 2
na = len(a)
k = 0
h = 0
x1 = 0#x0
relparams = [0,0,0,0,0,0,0,0]
m = 8
profile = 1
nlines = 1
nref = 0
deltax = 0
#Conversions
a = np.asarray(a)
relparams = np.asarray(relparams, dtype = np.int32)
#Call Dll
fit_dll = cdll.LoadLibrary("mydll.dll")
#Loop dll
#Setting argtypes
fit_dll.FDIR.argtypes = [POINTER(c_double), #1 - x
POINTER(c_double),#2 - a
POINTER(c_int),#3 - na
POINTER(c_int),#4 - k
POINTER(c_double),#5 - h
POINTER(c_double), #6 - x1
POINTER(c_int), #7 - relparam
POINTER(c_int), #8 - m
POINTER(c_int), #9 - profile
POINTER(c_int),#10 - nlines
POINTER(c_int),#11 - nref
POINTER(c_double)] #12 - deltax
# Setting restype
fit_dll.FDIR.restype = c_double
# Define Types
LP_c_double = POINTER(c_double)
LP_c_int = POINTER(c_int)
#Loop dll
print (LP_c_double(c_double(deltax)))
x_ = 0
fit_dll.FDIR(LP_c_double(c_double(x_)), #1
a.ctypes.data_as(POINTER(c_double)), #2
LP_c_int(c_int(na)), #3
LP_c_int(c_int(k)), #4
LP_c_double(c_double(h)),#5
LP_c_double(c_double(x1)), #6
relparams.ctypes.data_as(POINTER(c_int)), #7
LP_c_int(c_int(m)), #8
LP_c_int(c_int(profile)), #9
LP_c_int(c_int(nlines)), #10
LP_c_int(c_int(nref)), #11
LP_c_double(c_double(deltax))) #12