ctypes

Is Python's ctypes.c_long 64 bit on 64 bit systems?

In C, long is 64 bit on a 64 bit system. Is this reflected in Python's ctypes module? ...

How do I emulate a dynamically sized C structure in Python using ctypes

I'm writing some python code to interact with a C DLL that uses structures extensively. One of those structures contains nested structures. I know that this is not a problem for the ctypes module. The problem is that there is an often used structure that, in C, is defined via macro because it contains an "static" length array that can...

P/Invoke Interop Assistant Llike Tool for Python ctypes or struct Modules

The P/Invoke Interop Assistant tool has been very helpful when I've had to marshal from c style structs into managed C# types. Are there any similar tools for the Python ctypes or struct modules? ...

Help me understand why my trivial use of Python's ctypes module is failing

I am trying to understand the Python "ctypes" module. I have put together a trivial example that -- ideally -- wraps the statvfs() function call. The code looks like this: from ctypes import * class struct_statvfs (Structure): _fields_ = [ ('f_bsize', c_ulong), ('f_frsize', c_ulong), ('f_blocks...

ctypes.Structure Modify _fields_ at Run Time

Is it possible to modify the fields definition of the ctypes.Structure after it's been imported? Something like: from ctypes import * class A_STRUCT(Structure): _fields_ = [("one",c_int)] A_STRUCT._fields_.append(("two",c_int)) x = A_STRUCT() print x.one print x.two Not surprisingly this fails with: 0 Traceback (most recent ...

Union-within-structure syntax in ctypes

Hi all - Quick question about ctypes syntax, as documentation for Unions isn't clear for a beginner like me. Say I want to implement an INPUT structure (see here): typedef struct tagINPUT { DWORD type; union { MOUSEINPUT mi; KEYBDINPUT ki; HARDWAREINPUT hi; } ; } INPUT, *PINPUT; Should I or do I need to chang...

Python ctypes not loading dynamic library on Mac OS X

I have a C++ library repeater.so that I can load from Python in Linux the following way: import numpy as np repeater = np.ctypeslib.load_library('librepeater.so', '.') However, when I compile the same library on Mac OS X (Snow Leopard, 32 bit) and get repeater.dylib, and then run the following in Py...

Python ctypes - dll function accepting structures crashes

I have to access a POS terminal under ms windows xp. I am using python 2.7. The crucial function in the DLL I load that does the payment accepts two pointer to structures, but it crashes returning 1 (Communication error) but without further messages. Please note that when the payment function is called, not all the elements of POSData st...

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

Hello, 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. } From python (with ctypes) I would like to call this function based on a list of python strings: def call_c( string_list ): li...

Intel Compiler and Python/ctypes/libffi

Hi everyone - I am having trouble building a working version of Python with ctypes using the Intel Compiler (11.1). The trouble is, that libffi under the ctypes module does not work properly when interfacing with e.g. OpenGL. Originally, libffi did not compile using the Intel Compilers as __int128_t is not defined, and I followed the w...

Ctypes Offset Into A Buffer

I have a string buffer: b = create_string_buffer(numb) where numb is a number of bytes. In my wrapper I need to splice up this buffer. When calling a function that expects a POINTER(c_char) I can do: myfunction(self, byref(b, offset)) but in a Structure: class mystruct(Structure): _fields_ = [("buf", POINTER(c_char))] I am unabl...

Problems accessing a variable which is casted into pointer to array of int with ctypes in python.

Hi there, I have C code which uses a variable data, which is a large 2d array created with malloc with variable size. Now I have to write an interface, so that the C functions can be called from within Python. I use ctypes for that. C code: FOO* pytrain(float **data){ FOO *foo = foo_autoTrain((float (*)[])data); return foo; } ...

python Ctype : problem with callback in making python custom library

The whole scenario is like this: there is a function setmessagelistener(a_structure,function_pointer) in my c library. i am writing a python library which is a wrapper on above mentioned c library using Ctypes. so what i did is something like this: def setlistener(a_structure,function_pointer) listenerDeclaration = CFUNCTYPE(c_void...

Python: using ctypes

Hi: I need to use Dll from python using ctypes but I read the tutorial and I don´t understand anything!! I wants to load the dll from path and access to its functions... SOS!! Thanks ...

Using python ctypes to call io_submit in Linux

I'm trying to call io_submit using python ctypes. The code I'm writing is supposed to work on both 32 and 64-bit Intel/AMD architectures, but here I'll focus on 64 bits. I have defined the following: def PADDED64(type, name1, name2): return [(name1, type), (name2, type)] def PADDEDptr64(type, name1, name2): return [(name1, ty...

ctypes behaving strangely in Python interpreter

I am having a funny issue with ctypes; while it seems to work in regular python scripts, when I use it in the interpreter with printf() it prints the length of the string after the string itself. A demo: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more info...

Blob object for python (ctypes), C++

Hallo! I want a blob object which I can pass around in python and from time to time give it to a C++ function to write to. ctypes seems the way to go but I have problem with the python standard functions. For example: >>> import ctypes >>> T=ctypes.c_byte * 1000 >>> blob = T() >>> ctypes.pointer(blob) <__main__.LP_c_byte_Array_1000 o...

Image bit manipulation in Python

Hello SO, I have an application that receives a pointer to JPEG data from a camera API wrapped with ctypes, converts it to a wx.Image, and displays the images as a movie. One of required features is to set two of the components of a pixel equal to the third. E.g, my pixel in RGB format is (100,200,255), I want to set the the R and B va...

ctypes.windll.user32.GetCursorInfo() - how can I manage this to work? [Python]

I have to get the information about the current mouse cursor from windows but I'm not managing to work this command... what should I do? Can someone post one example? ...

Accessing the content of a variable array with ctypes.

Hi there, I use ctypes to access a file reading C function in python. As the read data is huge and unknown in size I use **float in C . int read_file(const char *file,int *n_,int *m_,float **data_) {...} The functions mallocs an 2d array, called data, of the appropriate size, here n and m, and copies the values to the referenced ones...