ctypes

Prototyping hybrid Python code

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 some core routines in a compiled language eventually. IIRC, one of Python's original remits was as a prototyping language, however Python ...

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? ...

Python ctypes and function calls

My friend produced a small proof-of-concept assembler that worked on x86. I decided to port it for x86_64 as well, but I immediately hit a problem. I wrote a small piece of program in C, then compiled and objdumped the code. After that I inserted it to my python script, therefore the x86_64 code is correct: from ctypes import cast, CFU...

Python: packing an ip address as a ctype.c_ulong() for use with DLL

given the following code: import ctypes ip="192.168.1.1" thisdll = ctypes.cdll['aDLL'] thisdll.functionThatExpectsAnIP(ip) how can I correctly pack this for a DLL that expects it as a c_ulong datatype? I've tried using: ip_netFrmt = socket.inet_aton(ip) ip_netFrmt_c = ctypes.c_ulong(ip_netFrmt) however, the c_ulong() method ...

Python: converting strings for use with ctypes.c_void_p()

given a string: msg="hello world" How can I define this as a ctypes.c_void_p() data type? the following code yields a "cannot be converted to pointer" exception: data=ctypes.c_void_p(msg) data is required to be a void* type in C, because it is being passed to a DLL. I'm assuming there is a way to pack/unpack the string using the...

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 Unfortunately I'm not sure what the correct way to unload the DLL is. _ctypes.FreeLibrary is available, but private. Is there some other way t...

Using Python's ctypes to pass/read a parameter declared as "struct_name *** param_name"?

I am trying to use Python's ctypes library to access some methods in the scanning library SANE. This is my first experience with ctypes and the first time I have had to deal with C datatypes in over a year so there is a fair learning curve here, but I think even without that this particular declaration would be troublesome: extern SANE...

Running unexported .dll functions with python

Hello! This may seem like a weird question, but I would like to know how I can run a function in a .dll from a memory 'signature'. I don't understand much about how it actually works, but I needed it badly. Its a way of running unexported functions from within a .dll, if you know the memory signature and adress of it. For example, I have...

long double returns and ctypes

i have a c function which returns a long double. i'd like to call this function from python using ctypes, and it mostly works. setting so.func.restype = c_longdouble does the trick -- except that python's float type is a c_double so if the returned value is larger than a double, but well within the bounds of a long double, python still...

How do I use ctypes to set a library's extern function pointer to a Python callback function?

Some C libraries export function pointers such that the user of the library sets that function pointer to the address of their own function to implement a hook or callback. In this example library liblibrary.so, how do I set library_hook to a Python function using ctypes? library.h: typedef int exported_function_t(char**, int); extern...

DataListItem to DropDownList or TextBox VB.Net

I have a DataListItem which can potentially be a dropdownlist or a textbox. To get the value I would need to do: CType(item.FindControl("myControl"), TextBox).Text Or CType(item.FindControl("myControl"), DropDownList).SelectedValue.ToString() The problem is, if it's a dropdownlist I get.. Unable to cast object of type 'Syste...

Using Python Ctypes for ssdeep's fuzzy.dll but receive Error [SOLVED]

I am trying to use python and ctypes to use the fuzzy.dll from ssdeep. So far all I have tried fails with and access violation error. Here is what I do after changing to the proper directory which contains the fuzzy.dll and fuzzy.def files. >>> import os,sys >>> from ctypes import * >>> fn = create_string_buffer(os.path.abspath("fuzzy...

Struct with a pointer to its own type in ctypes

I'm trying to map a struct definition using ctypes: struct attrl { struct attrl *next; char *name; char *resource; char *value; }; I'm unsure what to do with the "next" field of the struct in the ctypes mapping. A definition like: class att...

Mapping a global variable from a shared library with ctypes

I'd like to map an int value pbs_errno declared as a global in the library libtorque.so using ctypes. Currently I can load the library like so: from ctypes import * libtorque = CDLL("libtorque.so") and have successfully mapped a bunch of the functions. However, for error checking purposes many of them set the pbs_errno variable so I ...

pygame is screwing up ctypes

import mymodule, ctypes #import pygame foo = ctypes.cdll.MyDll.foo print 'success' if i uncomment the import pygame this fails with WindowsError: [Errno 182] The operating system cannot load %1. the stack frame is in ctypes python code, trying to load MyDll. win32 error code 182 is ERROR_INVALID_ORDINAL. if the pygame import is no...

communication with long running tasks in python

I have a python GUI app that uses a long running function from a .so/.dll it calls through ctypes. I'm looking for a way to communicate with the function while it's running in a separate thread or process, so that I can request it to terminate early (which requires some work on the C side before returning a partial result). I suppose th...

Using python ctypes to get buffer of floats from shared library into python string

I'm trying to use python ctypes to use these two C functions from a shared library: bool decompress_rgb(unsigned char *data, long dataLen, int scale) float* getRgbBuffer() The first function is working fine. I can tell by putting some debug code in the shared library and checking the input. The problem is getting the data out. The RG...

Turning ctypes data into python string as quickly as possible

I'm trying to write a video application in PyQt4 and I've used Python ctypes to hook into an old legacy video decoder library. The library gives me 32-bit ARGB data and I need to turn that into a QImage. I've got it working as follows: # Copy the rgb image data from the pointer into the buffer memmove(self.rgb_buffer, self.rgb_buffer_p...

ctypes bindings for Subversion in windows

Is there a binary installer or a faq for the new ctypes bindings for Subversion 1.6 in Windows (32 and 64bit)? What library would you use to make an easy to deploy (both win32 and x64) svn client in python for svn version >= 1.5? ...

python ctypes and sysctl

I have following code import sys from ctypes import * from ctypes.util import find_library libc = cdll.LoadLibrary(find_library("c")) CTL_KERN = 1 KERN_SHMMAX = 34 sysctl_names = { 'memory_shared_buffers' : (CTL_KERN, KERN_SHMMAX), } def posix_sysctl_long(name): _mem = c_uint64(0) _arr = c_int * 2 _name = _arr() ...