Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CTYPES: Procedure probably called with too many arguments (92 bytes in excess)

Tags:

python

dll

ctypes

I have a dll with

__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)

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]

I filled pointer with data

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

then I called

# 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))

and I got

ValueError: Procedure probably called with too many arguments (92 bytes in excess)

like image 934
venergiac Avatar asked Jan 20 '17 10:01

venergiac


1 Answers

if you are using windll.LoadLibrary, try to use instead CDLL.

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.

For a C-function like this:

__declspec(dllexport) int __cdecl wrp_testchar(int steps, double in_data[], char* in_char )

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)
like image 111
ValerioMantini Avatar answered Oct 18 '22 15:10

ValerioMantini