Questions tagged [ctypes]

`ctypes` is a Python package that wraps C .dll/.so libraries in pure Python.

ctypes is a Python package that wraps C .dll/.so libraries in pure Python. It works with Python 2.3 and higher. Since Python 2.5 ctypes has been included in the standard Python distribution.

ctypes provides functionality to call C functions in shared libraries, manipulate C types and data structures, and even allows C callbacks to be written in Python.

ctypes works in Windows, Linux, and many other OSs.

See http://docs.python.org/library/ctypes.html for more information.

3470 questions
292
votes
12 answers

Wrapping a C library in Python: C, Cython or ctypes?

I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices: Create an actual extension module in C. Probably overkill,…
balpha
  • 45,794
  • 14
  • 108
  • 128
114
votes
3 answers

ctypes - Beginner

I have the task of "wrapping" a c library into a python class. The docs are incredibly vague on this matter. It seems they expect only advanced python users would implement ctypes. Well i'm a beginner in python and need help. Some step by step…
spentak
  • 4,277
  • 15
  • 59
  • 90
69
votes
7 answers

ctypes error: libdc1394 error: Failed to initialize libdc1394

I'm trying to compile my program to a shared library that I can use from within Python code using ctypes. The library compiles fine using this command: g++ -shared -Wl,-soname,mylib -O3 -o mylib.so -fPIC [files] `pkg-config --libs --cflags…
fredley
  • 29,323
  • 39
  • 131
  • 223
60
votes
2 answers

Passing Numpy arrays to a C function for input and output

Oh my word I'm a fool. I was simply omitting the second and third arguments when calling the function. Like a fool. Because that's what I am. Original silly question follows: This seems like it must be a very common thing to do, but I can't find a…
Tom Future
  • 1,720
  • 2
  • 14
  • 15
59
votes
10 answers

Python: SWIG vs ctypes

In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the performance metrics of the two?
Kevin Little
  • 11,232
  • 5
  • 35
  • 46
58
votes
5 answers

How to use C++ classes with ctypes?

I'm just getting started with ctypes and would like to use a C++ class that I have exported in a dll file from within python using ctypes. So lets say my C++ code looks something like this: class MyClass { public: int test(); ... I would know…
jörg
  • 3,031
  • 3
  • 18
  • 12
55
votes
3 answers

Can I access ImageMagick API with Python?

I need to use ImageMagick as PIL does not have the amount of image functionality available that I am looking for. However, I am wanting to use Python. The python bindings (PythonMagick) have not been updated since 2009. The only thing I have been…
bsktball11ch
  • 683
  • 1
  • 6
  • 6
51
votes
4 answers

How do I convert a Python list into a C array by using ctypes?

If I have the follow 2 sets of code, how do I glue them together? void c_function(void *ptr) { int i; for (i = 0; i < 10; i++) { printf("%p", ptr[i]); } return; } def python_routine(y): x = [] for e in y: …
No One in Particular
  • 2,586
  • 3
  • 24
  • 32
46
votes
5 answers

How do I prevent a C shared library to print on stdout in python?

I work with a python lib that imports a C shared library that prints on stdout. I want a clean output in order to use it with pipes or to redirect in files. The prints are done outside of python, in the shared library. At the beginning, my approach…
user48678
  • 1,833
  • 2
  • 20
  • 29
45
votes
3 answers

Error loading DLL in python, not a valid win32 application

I am trying to load a DLL in python to call functions. import ctypes from ctypes import * dsusb = ctypes.WinDLL('c:\python27\dsusb.dll') I get the following error in my stack. C:\Python27>python test.py Traceback (most recent call last): File…
jeffpkamp
  • 2,355
  • 2
  • 22
  • 42
45
votes
3 answers

How to pack and unpack using ctypes (Structure <-> str)

This might be a silly question but I couldn't find a good answer in the docs or anywhere. If I use struct to define a binary structure, the struct has 2 symmetrical methods for serialization and deserialization (pack and unpack) but it seems ctypes…
Mr Temp
  • 453
  • 1
  • 4
  • 4
42
votes
13 answers

WindowsError: [Error 126] The specified module could not be found

I am loading a dll in python using following code: if os.path.exists(dll_path): my_dll = ctypes.cdll.LoadLibrary(dll_path) But I am continuously getting the following error WindowsError: [Error 126] The specified module could not be found dll…
MA1
  • 2,559
  • 5
  • 31
  • 49
41
votes
5 answers

Changing LD_LIBRARY_PATH at runtime for ctypes

How do you update this environment variable at runtime so that ctypes can load a library wherever? I've tried the following and neither seem to work. from ctypes import * os.environ['LD_LIBRARY_PATH'] = "/home/starlon/Projects/pyCFA635/lib" …
Scott
  • 4,685
  • 10
  • 52
  • 72
41
votes
4 answers

Pointers and arrays in Python ctypes

I have a DLL containing a C function with a prototype like this: int c_read_block(uint32 addr, uint32 *buf, uint32 num); I want to call it from Python using ctypes. The function expects a pointer to a chunk of memory, into which it will write the…
Andrew Bainbridge
  • 4,223
  • 3
  • 31
  • 46
40
votes
8 answers

list exported functions from dll with ctypes

Is there any way to know which functions are exported from the dll through python foreign function library ctypes? And if possible to know details about the exported functions through ctypes. If yes, could someone provide a snippet of code?
Tom
  • 461
  • 1
  • 4
  • 4
1
2 3
99 100