ctypes is a python built-in library that invokes exported functions from native compiled libraries.
Note: Since this library handles compiled code, it is relatively OS dependent.
Basic ctypes object
The most basic object is an int:
obj refers to a chunk of memory containing the value 12.
That value can be accessed directly, and even modified:
obj refers to a chunk of memory, we can also find out it's size and location:
Let's say we want to use
First, we must load
Then, we get the function object:
And now, we can simply invoke the function:
Which does exactly what we expect it to do.
Failing to load a file
The first possible error is failing to load the library. In that case an OSError is usually raised.
This is either because the file doesn't exists (or can't be found by the OS):
As you can see, the error is clear and pretty indicative.
The second reason is that the file is found, but is not of the correct format.
In this case, the file is a script file and not a
.so file. This might also happen when trying to open a
.dll file on a Linux machine or a 64bit file on a 32bit python interpreter. As you can see, in this case the error is a bit more vague, and requires some digging around.
Failing to access a function
Assuming we successfully loaded the
.so file, we then need to access our function like we've done on the first example.
When a non-existing function is used, an
AttributeError is raised:
Let's combine all of the examples above into one complex scenario: using
For more details about the function, read the man page. I urge you to read it before going on.
First, we'll define the proper prototypes:
Then, let's create the variables:
And now we define the comparison function:
POINTER(c_int), so we need to dereference them and take their values in order to actually compare the value stored in the memory.
Now we can combine everything together:
ptr is the returned void pointer. If
key wasn't found in
arr, the value would be
None, but in this case we got a valid value.
Now we can convert it and access the value:
Also, we can see that
ptr points to the correct value inside
As any good C programmer knows, a single value won't get you that far. What will really get us going are arrays!
This is not an actual array, but it's pretty darn close! We created a class that denotes an array of 16
Now all we need to do is to initialize it:
arr is an actual array that contains the numbers from 0 to 15.
They can be accessed just like any list:
And just like any other
ctypes object, it also has a size and a location:
Wrapping functions for ctypes
In some cases, a C function accepts a function pointer. As avid
ctypes users, we would like to use those functions, and even pass python function as arguments.
Let's define a function:
Now, that function takes two arguments and returns a result of the same type. For the sake of the example, let's assume that type is an int.
Like we did on the array example, we can define an object that denotes that prototype:
That prototype denotes a function that returns an
c_int (the first argument), and accepts two
c_int arguments (the other arguments).
Now let's wrap the function:
Function prototypes have on more usage: They can wrap
ctypes function (like
libc.ntohl) and verify that the correct arguments are used when invoking the function.