ctypes help

Mark Tolonen M8R-yfto6h at mailinator.com
Fri Jun 6 23:36:26 EDT 2008


"gianluca" <geonomica at gmail.com> wrote in message 
news:e1836378-5a00-43f5-b672-2e3a4191f960 at m73g2000hsh.googlegroups.com...
> hy,
> I've a huge problem with ctypes. I've compiled my C library and I'd
> like use it in python with ctype. One function need to pass a pointer
> to typed ( like this: typedef int value_type). In python I can access
> to that funtion but arise an exception  because python don't know my
> value_type typedef.
>
> Please I need help, could anybody help me?
>
> Gianluca

Let's see if I understand you correctly.  You have a typedef, and a function 
that is passed a pointer to that typedef.  Below is a short Windows DLL, 
compiled with "cl /LD example.c"

    typedef int value_type;

    __declspec(dllexport) void function(value_type* x)
    {
            *x *= 2;
    }

And here is some python code to call it:

>>> from ctypes import *
>>> value_type = c_int                      # declares a type "value_type" 
>>> as a ctypes integer
>>> value=value_type(5)                   # create an instance of value_type
>>> function=cdll.example.function  # look up the function and declare 
>>> return type and arguments
>>> function.restype=None
>>> function.argtypes=[POINTER(value_type)]
>>> function(pointer(value))              # call the function with a pointer 
>>> to the instance
>>> value
c_long(10)

Hope that helps.
-Mark




More information about the Python-list mailing list