@@ -771,6 +771,7 @@ static int _call_function_pointer(int flags,
771771 ffi_cif cif ;
772772 int cc ;
773773#ifdef MS_WIN32
774+ int delta ;
774775#ifndef DONT_USE_SEH
775776 DWORD dwExceptionCode = 0 ;
776777 EXCEPTION_RECORD record ;
@@ -821,8 +822,9 @@ static int _call_function_pointer(int flags,
821822#ifndef DONT_USE_SEH
822823 __try {
823824#endif
825+ delta =
824826#endif
825- ffi_call (& cif , (void * )pProc , resmem , avalues );
827+ ffi_call (& cif , (void * )pProc , resmem , avalues );
826828#ifdef MS_WIN32
827829#ifndef DONT_USE_SEH
828830 }
@@ -854,6 +856,35 @@ static int _call_function_pointer(int flags,
854856 return -1 ;
855857 }
856858#endif
859+ #ifdef MS_WIN64
860+ if (delta != 0 ) {
861+ PyErr_Format (PyExc_RuntimeError ,
862+ "ffi_call failed with code %d" ,
863+ delta );
864+ return -1 ;
865+ }
866+ #else
867+ if (delta < 0 ) {
868+ if (flags & FUNCFLAG_CDECL )
869+ PyErr_Format (PyExc_ValueError ,
870+ "Procedure called with not enough "
871+ "arguments (%d bytes missing) "
872+ "or wrong calling convention" ,
873+ - delta );
874+ else
875+ PyErr_Format (PyExc_ValueError ,
876+ "Procedure probably called with not enough "
877+ "arguments (%d bytes missing)" ,
878+ - delta );
879+ return -1 ;
880+ } else if (delta > 0 ) {
881+ PyErr_Format (PyExc_ValueError ,
882+ "Procedure probably called with too many "
883+ "arguments (%d bytes in excess)" ,
884+ delta );
885+ return -1 ;
886+ }
887+ #endif
857888#endif
858889 if ((flags & FUNCFLAG_PYTHONAPI ) && PyErr_Occurred ())
859890 return -1 ;
@@ -1130,7 +1161,11 @@ PyObject *_ctypes_callproc(PPROC pProc,
11301161 }
11311162 for (i = 0 ; i < argcount ; ++ i ) {
11321163 atypes [i ] = args [i ].ffi_type ;
1133- if (atypes [i ]-> type == FFI_TYPE_STRUCT )
1164+ if (atypes [i ]-> type == FFI_TYPE_STRUCT
1165+ #ifdef _WIN64
1166+ && atypes [i ]-> size <= sizeof (void * )
1167+ #endif
1168+ )
11341169 avalues [i ] = (void * )args [i ].value .p ;
11351170 else
11361171 avalues [i ] = (void * )& args [i ].value ;
0 commit comments