If you are on Linux, there is a handy utility nm to list the content of a shared library (there is always a handy utility on Linux, especially for C stuff).

Here is the question about it.

You use it with the -D flag: nm -D ./libMyLib.so

Answer from xealits on Stack Overflow
🌐
Python
docs.python.org › 3 › library › ctypes.html
ctypes — A foreign function library for Python
The ctypes.util module provides the dllist() function, which calls the different APIs provided by the various platforms to help determine which shared libraries have already been loaded into the current process.
🌐
Python.org
discuss.python.org › ideas
A `ctypes` function to list all loaded shared libraries - Ideas - Discussions on Python.org
October 16, 2023 - When writing code which loads dynamic libraries, it is often very useful to be able to query which shared libraries are already in use by the current process. There are a few well-known tricks to do this, but they all end up being platform dependent. For example, on Linux, you can use ...
🌐
GitHub
github.com › python › cpython › issues › 119349
Add a ctypes.util function to list loaded shared libraries · Issue #119349 · python/cpython
May 21, 2024 - Julia provides this function in the standard library under Libdl.dllist. A Python re-implementation of the same platform-specific code can be found at GitHub - WardBrian/dllist: List DLLs loaded by the current process .
Author: python
🌐
GitHub
github.com › python › cpython › issues › 113388
ctypes cdll list exported functions · Issue #113388 · python/cpython
December 22, 2023 - ctypes cdll list exported functions#113388 · Copy link · Labels · topic-ctypestype-featureA feature request or enhancementA feature request or enhancement · Zibri · opened · on Dec 22, 2023 · Issue body actions · after loading a library with cdll.LoadLibrary it would be great (and much requested) to have the exported function list available..
Author: python
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 427640 › accessing-functions-from-a-dll-using-python-ctypes
accessing functions from a dll using python ctypes
from ctypes import * device_list_size = 5 device_list = [] dll = cdll.LoadLibrary("C:\Sdk.dll") session = dll.CreateSession() dev_list = dll.GetAvailableDevices(session, pointer(device_list), pointer(device_list_size)) When I run the above i get : *** TypeError: type must have storage info · I am new to ctypes so not sure if this is correct, I would really appreciate if you could tell me the correct way of calling the above function.
🌐
Bytes
bytes.com › home › forum › topic › python
ctypes listing dll functions - Post.Byes
September 8, 2006 - As far as I know, there is no way with ctypes to enumerate the functions exported by a DLL.
Top answer
1 of 1
12

How do I code a C function to accept a list as its arguments?

Short answer, you can't.

Long answer: C does not have lists, but has arrays and pointers.

You have several options then:

int c_func(const char *dir, float abcd[4]) { // using an array with explicit size

int c_func(const char *dir, float abcd[]) { // Using an array (will decay to a pointer at compile-time)

int c_func(const char *dir, float *abcd) { // Using a pointer.

If you will only ever receive 4 floats, I'd suggest the first form, which enforces the size of the array, any user (and mainly your future self) of your function will know to give only an array of four elements.

Calling your function from Python:

floats = [1.0, 2.0, 3.0, 4.0] # Or whatever values you want to pass, but be sure to give only 4
FloatArray4 = ctypes.c_float * 4 # Define a 4-length array of floats
parameter_array = FloatArray4(*floats) # Define the actual array to pass to your C function

I don't know if passing more than 4 floats to FloatArray4 raises an error -- I guess so, but I can't check right now.

As for your second question, if you want dynamic sized arrays (more than 4 elements), you'll have to you one of the other two profiles for your C function, in which case I advise you to put an extra int argument for the size of the array:

int c_func(const char *dir, float floats[], int size) {

or

int c_func(const char *dir, float *floats, int size) {

You can also use the standard size_t instead of int, it's designed just for that.

I you want to pass a multidimensional array, you add another pair of brackets:

int c_func(const char *dir, float floats[][4]) { // Note the explicit size for second dimension

but remember that for a C array, for all dimensions but the first, the dimensions must be explicitly specified. If the value is constant it wont be an issue, however if not you will have to use a pointer instead:

int c_func(const char *dir, float *floats[]) {

or

int c_func(const char *dir, float **floats) {

which are two identical syntaxs (the array will decay to a pointer). Once again, if your dimensions are dynamic, I suggest to add parameters for the size.

If you want supplementary dimensions, just repeat that last step.

Find elsewhere
🌐
DevTut
devtut.github.io › python › ctypes.html
Python - ctypes
Let's say we want to use libc's ntohl function. ... >>> from ctypes import * >>> libc = cdll.LoadLibrary('libc.so.6') >>> libc <CDLL 'libc.so.6', handle baadf00d at 0xdeadbeef>
🌐
Samuelstevens
samuelstevens.me › writing › optimizing-python-code-with-ctypes
Optimizing Python Code with ctypes
lcsmodule = ctypes.cdll.LoadLibrary('lcsmodule/lcs.so') lcsmodule.lcs.argtypes = [ ctypes.POINTER(SEQUENCE), ctypes.POINTER(SEQUENCE), ] lcsmodule.lcs.restype = ctypes.POINTER(SEQUENCE) Finally, you can use your new C function to speed up your Python code. def list_to_SEQUENCE(strlist: List[str]) -> SEQUENCE: bytelist = [bytes(s, 'utf-8') for s in strlist] arr = (ctypes.c_char_p * len(bytelist))() arr[:] = bytelist return SEQUENCE(arr, len(bytelist)) def lcs(s1: List[str], s2: List[str]) -> List[str]: seq1 = list_to_SEQUENCE(s1) seq2 = list_to_SEQUENCE(s2) # struct Sequence *lcs(struct Sequence *s1, struct Sequence *s2) common = lcsmodule.lcs(ctypes.byref(seq1), ctypes.byref(seq2))[0] ret = [] for i in range(common.length): ret.append(common.items[i].decode('utf-8')) # manually free memory from c now that # it's referenced by the Python VM.
🌐
W3Schools
w3schools.com › python › ref_module_ctypes.asp
Python ctypes Module
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The ctypes module provides C compatible data types and allows calling functions ...
🌐
Nesi
nesi.github.io › perf-training › python-scatter › ctypes
Calling C/C++ extensions with ctypes
tell the argument and result types of the function. The argument types are listed in members argtypes (a list) and restype, respectively. Use for instance ctypes.c_int for a C int.
🌐
University of North Carolina
cs.unc.edu › ~gb › blog › 2007 › 02 › 11 › ctypes-tricks
ctypes tricks
February 11, 2007 - Suppose I have a foreign function foo that expects an array of ints. I could say: ary = (c_int * 3)() ary[0] = 1 ary[1] = 2 ary[2] = 3 foo(ary) I can replace an argument of type POINTER(c_int) with LIstPOINTER(c_int). Now I can call foo like: ... The idea above can easily be extended to replace an int** with a nested list. class ListPOINTER2(object): '''Just like POINTER(POINTER(ctype)) but accept a list of lists of ctype''' def __init__(self, etype): self.etype = etype def from_param(self, param): if isinstance(param, (list,tuple)): val = (POINTER(self.etype) * len(param))() for i,v in enumerate(param): if isinstance(v, (list,tuple)): val[i] = (self.etype * len(v))(*v) else: raise TypeError, 'nested list or tuple required at %d' % i return val else: raise TypeError, 'list or tuple required'
🌐
SciPy
scipy.github.io › old-wiki › pages › Cookbook › Ctypes.html
Cookbook/Ctypes - SciPy wiki dump
Before ctypes calls a C function, it uses the argtypes list to check each parameter.
🌐
Solarian Programmer
solarianprogrammer.com › 2019 › 07 › 18 › python-using-c-cpp-libraries-ctypes
Python - using C and C++ libraries with ctypes | Solarian Programmer
July 18, 2019 - 3 # 4 5 class CBMP(ctypes.Structure): 6 _fields_ = [ 7 ('handle', ctypes.c_void_p), 8 ('width', ctypes.c_int32), 9 ('height', ctypes.c_int32), 10 ('channels', ctypes.c_int32) 11 ] What is interesting in the above code is that we can map the names of the C structure and data types by using a list of pairs name type. Next, we can write the wrapping code for the six functions declared in cbmp.h:
🌐
Python
docs.python.org › 2.5 › lib › ctypes-accessing-functions-from-loaded-dlls.html
14.14.1.2 Accessing functions from loaded dlls
December 23, 2008 - Functions are accessed as attributes of dll objects: >>> from ctypes import * >>> libc.printf <_FuncPtr object at 0x...> >>> print windll.kernel32.GetModuleHandleA # doctest: +WINDOWS <_FuncPtr object at 0x...> >>> print windll.kernel32.MyOwnFunction # doctest: +WINDOWS Traceback (most recent ...
🌐
Yizhang82
yizhang82.dev › python-interop-inside-ctypes
Calling C functions from Python - part 3 - deep dive into ctypes implementation in CPython | yizhang82’s blog
January 22, 2018 - In last section we discussed the how library are loaded and we didn’t talk about functions yet. Functions are presented as _FuncPtr which is basically a _CFuncPtr in _ctypes module: