Questions tagged [ctypes]

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

ctypes
Filter by
Sorted by
Tagged with
318 votes
12 answers
91k views

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 ...
balpha's user avatar
  • 50.5k
138 votes
3 answers
142k views

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. Some step ...
spentak's user avatar
  • 4,677
74 votes
5 answers
68k views

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 ...
jörg's user avatar
  • 3,281
69 votes
7 answers
58k views

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 -...
fredley's user avatar
  • 33.3k
67 votes
2 answers
39k views

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 ...
Tom Future's user avatar
  • 1,920
64 votes
10 answers
27k views

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 ...
Kevin Little's user avatar
  • 12.7k
64 votes
4 answers
77k views

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 ...
No One in Particular's user avatar
62 votes
3 answers
69k views

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 ...
bsktball11ch's user avatar
56 votes
8 answers
20k views

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 ...
user48678's user avatar
  • 2,502
52 votes
8 answers
62k views

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,...
Tom's user avatar
  • 581
51 votes
15 answers
157k views

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: [...
MA1's user avatar
  • 2,797
49 votes
5 answers
49k views

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']...
Scott's user avatar
  • 5,215
49 votes
4 answers
37k views

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 ...
Mr Temp's user avatar
  • 493
49 votes
3 answers
45k views

Error loading DLL in python, not a valid win32 application [duplicate]

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>...
jeffpkamp's user avatar
  • 2,826
45 votes
3 answers
82k views

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 ...
Andrew Bainbridge's user avatar
44 votes
6 answers
39k views

Getting data from ctypes array into numpy

I am using a Python (via ctypes) wrapped C library to run a series of computation. At different stages of the running, I want to get data into Python, and specifically numpy arrays. The wrapping I ...
dtlussier's user avatar
  • 3,068
43 votes
6 answers
69k views

Access memory address in python

My question is: How can I read the content of a memory address in python? example: ptr = id(7) I want to read the content of memory pointed by ptr. Thanks.
Academia's user avatar
  • 4,024
41 votes
3 answers
34k views

How to convert ctypes' c_long to Python's int?

int(c_long(1)) doesn't work.
DSblizzard's user avatar
  • 4,067
40 votes
3 answers
56k views

Converting python string object to c char* using ctypes

I am trying to send 2 strings from Python (3.2) to C using ctypes. This is a small part of my project on my Raspberry Pi. To test if the C function received the strings correctly, I place one of them ...
LittleOne's user avatar
  • 1,107
39 votes
2 answers
7k views

Why is printf() giving a strange output in python?

I tried to use the C-function printf() in the python command line on Linux. To make that work, I imported ctypes. My problem is: If I create an object of CDLL to use the printf()-function in a loop, I ...
Aemyl's user avatar
  • 1,511
36 votes
6 answers
59k views

Calling C functions in Python

I have a bunch of functions that I've written in C and I'd like some code I've written in Python to be able to access those functions. I've read several questions on here that deal with a similar ...
Kitchi's user avatar
  • 1,904
31 votes
5 answers
50k views

Python ctypes: loading DLL from from a relative path

I have a Python module, wrapper.py, that wraps a C DLL. The DLL lies in the same folder as the module. Therefore, I use the following code to load it: myDll = ctypes.CDLL("MyCDLL.dll") This works if ...
G S's user avatar
  • 36.1k
31 votes
6 answers
5k views

Safer way to expose a C-allocated memory buffer using numpy/ctypes?

I'm writing Python bindings for a C library that uses shared memory buffers to store its internal state. The allocation and freeing of these buffers is done outside of Python by the library itself, ...
ali_m's user avatar
  • 73.1k
31 votes
3 answers
4k views

Building a ctypes-"based" C library with distutils

Following this recommendation, I have written a native C extension library to optimise part of a Python module via ctypes. I chose ctypes over writing a CPython-native library because it was quicker ...
Robie Basak's user avatar
  • 6,610
30 votes
2 answers
2k views

Clean way of structuring ctypes class

I've defined a ctypes class and an associated convenience function like so: class BNG_FFITuple(Structure): _fields_ = [("a", c_uint32), ("b", c_uint32)] class BNG_FFIArray(...
urschrei's user avatar
  • 26k
29 votes
2 answers
28k views

How to return array from C++ function to Python using ctypes

I am using ctypes to implement a C++ function in Python. The C++ function should return a pointer to an array. Unfortunately I haven't figured out, how to access the array in Python. I tried numpy....
dolby's user avatar
  • 663
29 votes
6 answers
17k views

How do I pass large numpy arrays between python subprocesses without saving to disk?

Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here's a cartoon example of what I'm hoping to accomplish: import sys, subprocess, numpy ...
Andrew's user avatar
  • 2,882
29 votes
1 answer
31k views

Python and ctypes: how to correctly pass "pointer-to-pointer" into DLL?

I have a DLL that allocates memory and returns it. Function in DLL is like this: void Foo( unsigned char** ppMem, int* pSize ) { * pSize = 4; * ppMem = malloc( * pSize ); for( int i = 0; i < ...
grigoryvp's user avatar
  • 41.3k
28 votes
6 answers
26k views

ctypes loading a c shared library that has dependencies

On Linux, I have a c shared library that depends on other libs. LD_LIBRARY_PATH is properly set to allow the linker to load all the libraries. When I do: libgidcwf = ctypes.cdll.LoadLibrary(...
zoobert's user avatar
  • 562
28 votes
3 answers
46k views

Python & Ctypes: Passing a struct to a function as a pointer to get back data

I've looked through other answers but can't seem to get this to work. I'm trying to call a function within a DLL for communicating with SMBus devices. This function takes a pointer to a struct, which ...
Jack's user avatar
  • 307
27 votes
1 answer
14k views

ctypes: construct pointer from arbitrary integer

For low-level purposes, I need to construct a ctypes pointer from an arbitrary address, given as an integer. For instance: INTP = ctypes.POINTER(ctypes.c_int) p = INTP(0x12345678) # i *know* this is ...
sebulba's user avatar
  • 1,275
27 votes
2 answers
9k views

ctypes vs C extension

I have a few functions written in C for a game project. These functions get called quite a lot (about 2000-4000 times per second). The functions are written in C for raw speed. Now, the easiest way ...
orlp's user avatar
  • 114k
26 votes
6 answers
31k views

How can I unload a DLL using ctypes in Python?

I'm using ctypes to load a DLL in Python. This works great. Now we'd like to be able to reload that DLL at runtime. The straightforward approach would seem to be: 1. Unload DLL 2. Load DLL ...
Tom Hennen's user avatar
  • 4,794
24 votes
6 answers
14k views

Passing a list of strings to from python/ctypes to C function expecting char **

I have a C function which expects a list \0 terminated strings as input: void external_C( int length , const char ** string_list) { // Inspect the content of string_list - but not modify it. } ...
Joakim Hove's user avatar
24 votes
4 answers
24k views

How do I interact with MATLAB from Python?

A friend asked me about creating a small web interface that accepts some inputs, sends them to MATLAB for number crunching and outputs the results. I'm a Python/Django developer by trade, so I can ...
Pete's user avatar
  • 2,583
24 votes
2 answers
2k views

Loading and accessing multiple ctype instances

I have some existing C code that I am working with in Python. I am able to load the library using the following commands: library_path = '/full/path/to/my/library.dylib' lib1 = cdll.LoadLibrary(...
Castrona's user avatar
  • 493
22 votes
7 answers
5k views

Prototyping with Python code before compiling

I have been mulling over writing a peak-fitting library for a while. I know Python fairly well and plan on implementing everything in Python to begin with but envisage that I may have to re-implement ...
Brendan's user avatar
  • 19k
22 votes
1 answer
6k views

Swig python - c++ how to use type int8_t

I have a C function that takes as paramenter an 8 bit integer int8_t foo( int8_t x ); I would like to call this function from my python code using a swig interface but int8_t type do not exists in ...
Stefano's user avatar
  • 4,011
21 votes
6 answers
57k views

Can't import dll module in Python

I've been stressin for a few days trying to compile a modified version of libuvc on windows and now that I've finally done it, I can't seem to load it on Python. This lib that I've already compiled ...
tigonza's user avatar
  • 247
21 votes
5 answers
2k views

Building Self-Referencing Tuples

After seeing a conversation in a forum from many years ago that was never resolved, it caused me to wonder how one would correctly create a tuple that referenced itself. Technically, this is a very ...
Noctis Skytower's user avatar
21 votes
1 answer
6k views

What is the difference between ctypes.pointer, ctypes.POINTER, and ctypes.byref?

In ctypes, what is the difference between pointer and byref? They both seem like a way to pass a pointer to a function, for example as an output parameter.
joeforker's user avatar
  • 41.2k
19 votes
2 answers
36k views

How to use NumPy array with ctypes?

I am still writing on a python interface for my c code with ctypes. Today I substituted my file reading function with a python version, which was programmed by somebody else using NumPy. The 'old' c ...
Framester's user avatar
  • 34.3k
19 votes
3 answers
36k views

Python | accessing dll using ctypes

I'm trying to access some functions in a dll (nss3.dll) that ships with Firefox web browser. To handle this task I have used ctypes in Python. The problem is that it fails at the initial point which ...
Switch's user avatar
  • 15.1k
19 votes
2 answers
18k views

Python ctypes: how to free memory? Getting invalid pointer error

I want to get some string from a C/C++ library with ctypes into python. My code looks like this: Code in lib: const char* get(struct something *x) { [...] // buf is a stringstream return ...
snøreven's user avatar
  • 1,954
19 votes
2 answers
12k views

Callbacks with ctypes (How to call a python function from C)

Is it possible to call a Python function from a C dll function? We consider this C function: void foo( void (*functionPtr)(int,int) , int a, int b); On Python, I would like to call foo and set the ...
nowox's user avatar
  • 27.4k
19 votes
1 answer
16k views

Different behaviour of ctypes c_char_p?

I am confused with this behaviour of different versions of python and dont understand why ? Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on ...
Sagar Masuti's user avatar
  • 1,281
19 votes
1 answer
23k views

How to use malloc and free with python ctypes?

I have a function in my C library, say runsim() which takes pointer to struct repdata as one of the arguments, where struct repdata is given by struct repdata { int *var1; int *var2; int *...
Curious2learn's user avatar
19 votes
1 answer
744 views

SWIG Python bindings to native code not working with OpenCV 2.1

I have an OpenCV project mixing Python and C. After changing to OpenCV 2.1, my calls to C code are not working any more, probably because OpenCV is no more using SWIG bindings. From Python, I was ...
TH.'s user avatar
  • 1,758
18 votes
1 answer
22k views

How to convert pointer to c array to python array

I have a C++ callback function that calls into Python using ctypes. This function's parameters are a pointer to an array of double and the number of elements. There are a lot of elements, ...
ochensati's user avatar
  • 359
18 votes
5 answers
82k views

Python import dll

How would I import a winDLL into python and be able to use all of its functions? It only needs doubles and strings.
pajm's user avatar
  • 1,808

1
2 3 4 5
84