0
votes

for example I have a C++ variable declaration and function like this(assume its a dll function)


    int img_width, img_height, stride;
    somefunction(&img_width, &img_height, &stride)
    {
    ....
    }

same thing how can we do using python ctypes?

I tried in the following way

img_width, img_height, stride = c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int)
dll.somefunction(img_width, img_height, stride)

which resulted the following exception

ctypes.ArgumentError: argument 1: : Don't know how to convert parameter 1)

I also tried in following way

dll.somefunction.restype = c.c_void_p
dll.somefunction.argtypes = [c.POINTER(c_int), c.POINTER(c_int), c.POINTER(c_int)]
dll.somefunction(img_width, img_height, stride)

Where I endup with following exception

NameError: global name 'img_width' is not defined
1

1 Answers

0
votes

You are creating types where you need to create instances of the integers and pass by reference:

from ctypes import *

# Declare the argument types
dll.somefunction.argtypes = POINTER(c_int), POINTER(c_int), POINTER(c_int)

# Create instances of the types needed
img_width, img_height, stride = c_int(), c_int(), c_int()

# Pass by reference
dll.somefunction(byref(img_width), byref(img_height), byref(stride))