ctypes

Calling gdc/dmd shared libraries from Python using ctypes

Hello, I've been playing around with the rather excellent ctypes library in Python recently. What i was wondering is, is it possible to create shared 'D' libraries and call them in the same way. I'm assuming i would compile the .so files using the -fPIC with dmd or gdc and call them the same way using the ctypes library. Has anyone tr...

Python double pointer

I'm trying to get the values from a pointer to a float array, but it returns as c_void_p in python The C code double v; const void *data; pa_stream_peek(s, &data, &length); v = ((const float*) data)[length / sizeof(float) -1]; Python so far import ctypes null_ptr = ctypes.c_void_p() pa_stream_peek(stream, null_ptr, ctypes.c_ulon...

Using ctypes.c_void_p as an input to glTexImage2D?

I'm using a 3rd party DLL to load in some raw image data, and I want to use this raw image data as a texture in openGL. However, the c function returns a void*, and I need to somehow convert this so it will work as the "pixels" parameter to glTexImage2D. Right now my code looks like something this: data = c_void_p(vdll.vlImageGetData(...

Subclassing ctypes - Python

This is some code I found on the internet. I'm not sure how it is meant to be used. I simply filled members with the enum keys/values and it works, but I'm curious what this metaclass is all about. I am assuming it has something to do with ctypes, but I can't find much information on subclassing ctypes. I know EnumerationType isn't doing...

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" os.putenv('LD_LIBRARY_PATH', "/home/starlon/Projects/pyCFA635/lib") lib = CDLL("libevaluator....

Some help with some Python code

Can anyone tell me why num_chars and num_rows have to be the same? from ctypes import * num_chars = 8 num_rows = 8 num_cols = 6 buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars) for char in range(num_chars): for row in range(num_rows): for col in range(num_cols): if ...

Python ctypes and function pointers

This is related to my other question, but I felt like I should ask it in a new question. Basically FLAC uses function pointers for callbacks, and to implement callbacks with ctypes, you use CFUNCTYPE to prototype them, and then you use the prototype() function to create them. The problem I have with this is that I figured that I would...

Looking for the "Hello World" of ctypes unicode processing (including both Python and C code)

Can someone show me a really simple Python ctypes example involving Unicode strings including the C code? Say, a way to take a Python Unicode string and pass it to a C function which catenates it with itself and returns that to Python, which prints it. ...

Resize ctypes array

I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't work like it could. I can write a function to resize an array, but I wanted to know some other solutions to this. Maybe I'm missing some ctypes trick or maybe I simply used resize wrong. The name c_long_Array_0 seems to tell me this may not work with resize. >>> fr...

ctypes in python, problem calling a function in a DLL

Hey! as you might have noticed I have an annoying issue with ctypes. I'm trying to communicate with an instrument and to do so I have to use ctypes to communicate with the DLL driver. so far I've managed to export the DLL by doing this: >>> from ctypes import * >>>maury = WinDLL( 'MLibTuners') >>> maury (WinDLL 'MlibTuners', handle 100...

ctypes in Python 2.6 help

I can't seem to get this code to work, I was under the impression I was doing this correctly. from ctypes import * kernel32 = windll.kernel32 string1 = "test" string2 = "test2" kernel32.MessageBox(None, string1, string2, MB_OK) ** I tried to change it to Message...

How to do cleanup reliably in python?

I have some ctypes bindings, and for each body.New I should call body.Free. The library I'm binding doesn't have allocation routines insulated out from the rest of the code (they can be called about anywhere there), and to use couple of useful features I need to make cyclic references. I think It'd solve if I'd find a reliable way to ho...

Some help understanding async USB operations with libusb-1.0 and ctypes

Alright. I figured it out. transfer.flags needed to be a byte instead of an int. Silly me. Now I'm getting an error code from ioctl, errno 16, which I think means the device is busy. What a workaholic. I've asked on the libusb mailing list. Below is what I have so far. This isn't really that much code. Most of it is ctypes structures fo...

USB - sync vs async vs semi-async (partially answered now)

Updates: I wrote an asynchronous C version and it works as it should. Turns out the speed issue was due to Python's GIL. There's a method to fine tune its behavior. sys.setcheckinterval(interval) Setting interval to zero (default is 100) fixes the slow speed issue. Now all that's left is to figure out is what's causing the other issu...

Guides for implementing a foreign function interface

Right now I'm working on a scripting language that doesn't yet have a FFI. I'd like to know what's the most convenient way to get it in, assuming that I'd like to write it like cool geeks do - I'd like to write the FFI in the scripting language itself. The programming language I need to interface is C. So for basics I know that libdl.so...

Problem running functions from a DLL file using ctypes in Object-oriented Python

Hello everyone! I sure hope this won't be an already answered question or a stupid one. Recently I've been programming with several instruments. Trying to communicate between them in order to create a testing program. However I've encoutered some problems with one specific instrument when I'm trying to call functions that I've "masked"...

IO completion port key confusion

I'm writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed at those who have a knowledge of IOCP, not Python. As I understand the documentation for CreateIoCompletionPort, you specify your "user...

python ctype recursive structures

I've developped a DLL for a driver in C. I wrote a test program in C++ and the DLL works fine. Now I'd like to interract with this DLL using Python. I've successfully hidden most of the user defined C structures but there is one point where I have to use C structures. I'm rather new to python so I may get things wrong. My approach is t...

How to read/copy ctype pointers into python class?

This is a kind of follow-up from my last question if this can help you. I'm defining a few ctype structures class EthercatDatagram(Structure): _fields_ = [("header", EthercatDatagramHeader), ("packet_data_length", c_int), ("packet_data", POINTER(c_ubyte)), ("work_count", c_ushort)] class EthercatPacket(Structu...

Why does import of ctypes raise ImportError?

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python26\lib\ctypes\__init__.py", line 17, in <module> from struct import ca...