ctypes

Python ctypes - how to handle arrays of strings

I'm trying to call an external library function that returns a NULL-terminated array of NULL-terminated strings. kernel32 = ctypes.windll.kernel32 buf = ctypes.create_unicode_buffer(1024) length = ctypes.c_int32() if kernel32.GetVolumePathNamesForVolumeNameW(ctypes.c_wchar_p(volume), buf, ctypes.sizeof(buf), ctypes.pointer(length)):...

Load a .so library into ctypes

I have compiled a library using cmake add_library(object3d SHARED some_file.h some_file.cpp). After compilation, I get a file: libobject3d.so I would like to call a function in this library. This function definition in some_file.h is: void ComputeGeometryImage(char * input_image, int geometry_image_size, float * output); I did check...

Wrapping C++ dynamic array with Python+ctypes, segfault

Hi! I wanted to wrap a small C++ code allocating an array with ctypes and there is something wrong with storing the address in a c_void_p object. (Note: the pointers are intentionally cast to void*, 'cause later I want to do the allocation the same way for arrays of C++ objects, too.) The C(++) functions to be wrapped: void* test_all...

Does ctypes provide anything for enums and flags?

I have an API I'd like to use from python. That API contains flags and enums implemented with #define. // it's just almost C so don't bother adding the typedef and parenthesis diarrhea here. routine(API_SOMETHING | API_OTHERTHING) stuff = getflags() ? stuff & API_SOMETHING action(API_INTERESTING) mode = getaction() ? mode == INTERESTIN...

python ctypes.WinDLL error , _dlopen(self._name, mode) cant be found

ctypes.WinDLL("C:\Program Files\AHSDK\bin\ahscript.dll") Traceback (most recent call last): File "", line 1, in File "C:\Python26\lib\ctypes_init_.py", line 353, in init self._handle = _dlopen(self._name, mode) WindowsError: [Error 126] The specified module could not be found how can i solve it ... i found the _dlopen in C:\Py...

Linking problem using OpenMp with ctypes.

Hi there, I have a c99 function that uses openmp, which works as expected. I also wrote a ptyhon interface using ctypes which causes the problem. Ctypes/python can not find the library for openmp. Here is the error massage: File "foo.py", line 2, in <module> foobar=cdll.LoadLibrary("./libfoo.so") File "/usr/lib/python2.6/ctypes/_...

How to declare a C struct with a pointer to array in ctypes?

Hi there, I read the official ctypes tutorial and also searched SO, but I could not find a way to declare this kind of structure with ctypes. This structure is returned by one of the functions I write an Python interface for. typedef struct{ int i; float *b1; float (*w1)[]; }foo; This is what I have so far: class foo(Str...

How to return a pointer to a structure in ctypes?

Hi, I try to pass a pointer of a structure which is given me as a return value from the function 'bar' to the function 'foo_write'. But I get the error message 'TypeError: must be a ctypes type' for line 'foo = POINTER(temp_foo)'. In the ctypes online help I found that 'ctypes.POINTER' only works with ctypes types. Do you know of anothe...

How to 'catch' c printf in python with ctypes?

Hi there, I hope this is trivial and I just didn't find it in the tutorials. I am writing python code that 'supervises' c code, aka I run the c code with ctypes from python. Now I want to 'catch' the c 'printfs' to process the data that is output by the c code. Any idea how one would do this? Thanks ...

ctypes outputting unknown value at end of correct values

Hi all - I have the following DLL ('arrayprint.dll') function that I want to use in Python via ctypes: __declspec(dllexport) void PrintArray(int* pArray) { int i; for(i = 0; i < 5; pArray++, i++) { printf("%d\n",*pArray); } } My Python script is as follows: from ctypes import * fiveintegers = c_int * 5 x = fivei...

ctypes initializing c_int array by reading file

Hi all - Using a Python array, I can initialize a 32,487,834 integer array (found in a file HR.DAT) using the following (not perfectly Pythonic, of course) commands: F = open('HR.DAT','rb') HR = array('I',F.read()) F.close() I need to do the same in ctypes. So far the best I have is: HR = c_int * 32487834 I'm not sure how to initi...

Why the id(string) does not return the address of string

import ctypes a = 'abc' b = ctypes.string_at(id(a), 3) c = ctypes.string_at(id(a) + 20, 3) I expect the result of b to be 'abc', but it is not; and the result of c is 'abc'. I don't know why. Anyone can explain me? ...

unknown array length in python ctypes

I'm calling a C function using ctypes from Python. It returns a pointer to a struct, in memory allocated by the library (the application calls another function to free it later). I'm having trouble figuring out how to massage the function call to fit with ctypes. The struct looks like: struct WLAN_INTERFACE_INFO_LIST { DWORD ...

How to use NumPy array with ctypes?

Hi there, 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 usind NumPy. The 'old' c version was called with a byref(p_data) while p_data=PFloat() (see below). The main function takes the p_data. Old file reading:...

How to call C functions from python?

I have a scenario where GUI (Developed in VB) is sending commands to the target system application developed in C language (Xilinx). User on a PC sends the commands to target using GUI. But now I need to remove the GUI and want to send commands (call C functions in target system application) using Python. I found some useful info from...

Back-casting a ctypes.py_object in a callback

I am trying to wrap a C library using ctypes. One feature of the library is an ondestroy callback which is called when a handle returned by the library is about to be destroyed. The callback has the signature: void cb(f *beingdestroyed); The API allows one to associate a user-specified void * with f when it is returned by the librar...

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 used to call a C function with the following prototype: int fast_support_transform(CvMat * I, CvMat * N,...); Now, I get the following err...

casting to Arrays in Python ctypes

I'm trying to convert a 16 byte blob of data returned by socket.inet_pton into a ctypes array of unsigned bytes. My data structure looks like this: class in6_addr(ctypes.Structure): _fields_ = (("Byte", ctypes.c_ubyte * 16),) And the blob is just: data = socket.inet_pton(socket.AF_INET6, "2001::3") However, these attempts get...

define *struct in ctypes

I need to convert regexitem *regex to ctype variable, any ideas? C function expects func(regexitem *regex) char *regex1Groups[] = { "a","b","x","s" ,NULL}; char *regex2Groups[] = { "l" ,NULL}; regexitem regex[] = { {"bla", regex1Groups,4 }, {"bla2",regex2Groups,1 } }; First i defined class ...

position contents of array.array into heap

I have a simple byte array I've filled with a x86 -program. Which I need to execute at runtime. """ Produces a simple callable procedure which returns a constant. """ from array import array simple = array('B') # mov rax, 0x10 simple.extend((0x81, 0xc0, 0x10, 0x0, 0x0, 0x0)) # ret simple.append(0xc3) Now, to get this running, I'...