Re: ctypes callback with char array



ohlfsen <ohlfsen@xxxxxxxxx> writes:

Hello.

Hoping that someone can shed some light on a tiny challenge of mine.

Through ctypes I'm calling a c DLL which requires me to implement a callback in Python/ctypes.

The signature of the callback is something like

void foo(int NoOfElements, char Elements[][100])

How do I possible implement/process "char Elements[][100]" in Python/ctypes code?

I'd try it like this:

$ python
Python 2.7 (r27:82500, May 2 2011, 22:50:11)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
oWelcome to rlcompleter2 0.98
for nice experiences hit <tab> multiple times
from ctypes import *
callback_type = CFUNCTYPE(None, c_int, POINTER(c_char_p))
def my_callback(int, elements):
.... pass
....
c_callback = callback_type(my_callback)
c_callback
<CFunctionType object at 0x100697940>


No need to know that it's a pointer to char[100] pointers - you can cast
that if you want to.

Diez
.



Relevant Pages

  • Re: Help please with pointers to callbacks in structs
    ... then the callback function can't change the ... Stream s = File.OpenWrite; ... >> If you set a pointer to text or to anything you should allocate memory ... >> The problems start when eg. c# allocates memory and the dll must free it ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: FPGA VI communicating directly to a Windows application on PC
    ... or dll) through a FIFO or queue. ... A callback is a good mechanism for this. ... So you need to be able to call a function at a given pointer. ... main application needs to tell the LV dll what this pointer is. ...
    (comp.lang.labview)
  • pass function pointer to dll
    ... I have used loadlibrary to successfully load a dll and can call the methods contained inside. ... But one of the methods expects a pointer to a function to use as a callback. ... Using libfunctions -full I can see that the argument for the function pointer is listed as a voidPtr. ... So in Matlab I have the function: ...
    (comp.soft-sys.matlab)
  • Re: static variables and dll/lib troubles
    ... Not clear why a callback would be needed. ... pointer to the value, which would normally never be manipulated outside the DLL. ... Factory class that stores function pointers of each other class' creation ...
    (microsoft.public.vc.mfc)
  • ctypes callback with char array
    ... Through ctypes I'm calling a c DLL which requires me to implement a callback in Python/ctypes. ...
    (comp.lang.python)