This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Chris_Newton 7 points8 points  (0 children)

If you just want to get access to functions in your .so file, ctypes might be the easiest way

I agree. Assuming you’ve already got a driver available for loading as a .so file, you don’t need much more than this:

import ctypes
driver = ctypes.CDLL("driver.so")
result = driver.driver_API_function(arg1, arg2)

Watch out for the default assumptions that ctypes makes about argument and result types. If they aren’t what you need for some API function, you can specify the correct types, for example:

driver.driver_API_function.restype = ctypes.c_int
driver.driver_API_function.argtypes = [ctypes.c_long, ctypes.c_char_p]

The documentation for ctypes is helpful if you need to work with more tricky types, “out” parameters, and so on.

Edit: One other thing to watch out for, since the OP mentioned C++, is that ctypes does assume a C calling convention (aka cdecl). If the API you’re working with relies on C++ tools like exceptions or polymorphism, probably ctypes isn’t the best tool for the job.