1

I need the following C function in Python:

int func(Uint8 *bytRecvBuffer, int *iRecvLen);

I tried in Python:

mydll = cdll.LoadLibrary("my.dll")

recvBuffer = None
recvLength = None

funcFunction = getattr(mydll, "func")
funcFunction(POINTER(c_ubyte(recvBuffer)), POINTER(c_int(recvLength)))

Error message:

TypeError: an integer is required

What is wrong here with the parameters?

4 Answers 4

2

You'll need to allocate an actual buffer and integer:

from ctypes import *
mydll = CDLL("my")
recvBuffer = create_string_buffer(1000)
recvLength = c_int(1000)
mydll.func(recvBuffer,byref(recvLength))

You can also declare the specific parameters supported by func, but then you must also be more explicit about the parameters passed:

mydll.func.argtypes = [POINTER(c_ubyte),POINTER(c_int)]
mydll.func.restype = c_int
recvBuffer = (c_ubyte*1000)()
recvLength = c_int(1000)
mydll.func(recvBuffer,byref(recvLength))
Sign up to request clarification or add additional context in comments.

Comments

2

None is not integer.

c_int(None) causes TypeError

Init your variables with 0, not None

Comments

0

Did you try loading the dll with windll instead of cdll? also....isn't it pointer instead of POINTER ?

1 Comment

@leon22, clarification. POINTER(type) takes a type and creates a pointer type for that type. pointer(instance) takes an instance and instantiates a pointer to that instance.
0

Guessing from your initialization of recvBuffer and recvLength you want to create null pointer.

In that case you should create an instance without argument of specific type, do the following:

recvBuffer = POINTER(c_ubyte)()   
recvLength = POINTER(c_int)()

funcFunction(recvBuffer, recvLength)

if you got the buffer within python, you need to cast your fixed size array to the correct pointer type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.