本文介绍了CTYPES:过程可能调用了太多参数(超出了92个字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 dll ,其中
__declspec(dllexport) int __cdecl wrp_ANN_sel_HDGT(int steps,
double in_TNH[], double in_TNL[], double in_pamb[], double in_Tamb[], double in_CPD[],
double in_T3[], double in_Tex[], double in_IGV[], double in_Dpin[], double in_Dpout[],
double in_LHV[], double in_RH[], short in_model, double in_pamb_ref, double in_Dpin_ref,
double in_Dpout_ref, double in_LHV_ref, double in_RH_ref, double out_Tfire[], double out_T39[], double out_Power[], char *RuleDllPath)
我定义了argtypes
I defined argtypes
# Specify the parameter and return types
hllDll.wrp_ANN_sel_HDGT.argtypes = [ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N), ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_short), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N),
ctypes.POINTER(ctypes.c_double * N),
ctypes.c_char_p]
我用数据填充了指针
RuleDllPath = ctypes.c_char_p(b'ANN_sel_HDGT_win32')
in_TNH = (ctypes.c_double * N)()
in_TNL = (ctypes.c_double * N)()
in_pamb = (ctypes.c_double * N)()
in_Tamb = (ctypes.c_double * N)()
in_CPD = (ctypes.c_double * N)()
in_T3 = (ctypes.c_double * N)()
in_Tex = (ctypes.c_double * N)()
in_IGV = (ctypes.c_double * N)()
in_Dpin = (ctypes.c_double * N)()
in_Dpout = (ctypes.c_double * N)()
in_LHV = (ctypes.c_double * N)()
in_RH = (ctypes.c_double * N)()
for i in range(0, N):
in_TNH[i] = float(SpeedH[i])
in_TNL[i] = float(SpeedL[i])
in_pamb[i] = float(Pamb[i])
in_Tamb[i] = float(Tamb[i])
in_CPD[i] = float(CPD[i])
in_T3[i] = float(CTD[i])
in_Tex[i] = float(TEX[i])
in_IGV[i] = float(IGV[i])
in_Dpin[i] = float(Inloss[i])
in_Dpout[i] = float(OutLoss[i])
in_LHV[i] = float(LHV[i])
in_RH[i] = float(ro[i])
然后我打电话
# Call function
a = wrp_ANN_sel_HDGT(ctypes.byref(steps), ctypes.byref(in_TNH), ctypes.byref(in_TNL), ctypes.byref(in_pamb),
ctypes.byref(in_Tamb), ctypes.byref(in_CPD), ctypes.byref(in_T3), ctypes.byref(in_Tex),
ctypes.byref(in_IGV), ctypes.byref(in_Dpin), ctypes.byref(in_Dpout), ctypes.byref(in_LHV),
ctypes.byref(in_RH),
ctypes.byref(in_model),
ctypes.byref(in_pamb_ref),
ctypes.byref(in_Dpin_ref), ctypes.byref(in_Dpout_ref), ctypes.byref(in_LHV_ref), ctypes.byref(in_RH_ref),
ctypes.byref(out_Tfire), ctypes.byref(out_T39), ctypes.byref(out_Power), (RuleDllPath))
我得到了
推荐答案
如果您使用的是 windll.LoadLibrary ,请尝试使用 CDLL 。
if you are using windll.LoadLibrary, try to use instead CDLL.
来自网络:
该函数的调用约定是x86 cdecl,但是您正在使用x86 stdcall约定(WinDLL)。
如果您使用的是32位Python,则可能是约定俗成的问题。
From web:"The function's calling convention is x86 cdecl, but you're using the x86 stdcall convention (WinDLL)." If you're using 32-bit Python, it could be a problem of convention.
对于这样的C函数:
__declspec(dllexport) int __cdecl wrp_testchar(int steps, double in_data[], char* in_char )
python代码必须为:
The python code must be:
N = 2
from ctypes import *
# Load the library
hllDll = CDLL("testchar.dll") wrp_testchar = hllDll.wrp_testchar
# Specify the parameter and return types
hllDll.wrp_testchar.argtypes = [POINTER(c_int), POINTER(c_double * N), c_char_p]
# Next, set the return types...
hllDll.wrp_testchar.restype = c_int
steps = (c_int)(2)
in_data = (c_double * N)()
in_data[0] = float(1)
in_data[1] = float(1)
in_char = (c_char_p)(b'Helloworld')
a = int(0)
# Call function
a = wrp_testchar(byref(steps), byref(in_data), in_char)
这篇关于CTYPES:过程可能调用了太多参数(超出了92个字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!