I'm trying to call a C function using python ctypes. I want to pass and receive a struct from the function but am having a lot of trouble with it.
I have a C dll with the following code:
-- test.h --
struct vec3f
{
float x;
float y;
float z;
};
__declspec(dllexport) struct vec3f addvec(struct vec3f a, struct vec3f b);
-- test.c --
struct vec3f addvec(struct vec3f a, struct vec3f b)
{
a.x += b.x;
a.y += b.y;
a.z += b.z;
return a;
}
My python code looks like this:
-- tester.py --
from ctypes import *
import os
libtest = cdll.LoadLibrary(os.getcwd() + '/libpydlltest.dll')
class vec3f(Structure):
_fields_ = [("x", c_float),
("y", c_float),
("z", c_float)]
libtest.addvec.argtypes = [vec3f, vec3f]
libtest.addvec.restype = vec3f
a = vec3f(10, 15, 20)
b = vec3f(4, 5, 6)
print a.x, a.y, a.z
print b.x, b.y, b.z
a = libtest.addvec(a,b)
print a.x, a.y, a.z
The problem is, when I run my python script, I get the following error:
Quote:
Traceback (most recent call last):
File "tester.py", line 20, in <module>
a = libtest.addvec(a,b)
ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong calling convention