Re: Calling a function with a custom datatype argument

"Martin (gzlist)" <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
On 15/01/2010, Rosalyn Hatcher <[email protected]> wrote:
>
> How do I create the ut_error_message_handler datatype in python and thus
> pass a correctly typed argument to the function?

Short answer: you can't, ctypes doesn't provide a way of dealing with
varargs functions.

There are a couple of ways to cheat though. Firstly, by just using the
function you can pass in any kind of pointer, so you can for instance
use printf from your c library as it has the same signature. Secondly,
you can misdeclare ut_error_message_handler omitting the va_list part
and use that as the signature for any functions you'll want to pass.

    printf_like = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
    ut_set_error_message_handler = ctypes.CFUNCTYPE(printf_like, printf_like)(
        ("ut_set_error_message_handler", udunits2))
    printf = printf_like(("printf", libc))
    ut_write_to_stderr = printf_like(("ut_write_to_stderr", udunits2))
    ut_ignore = printf_like(("ut_ignore", udunits2))

    old_handler = ut_set_error_message_handler(ut_write_to_stderr)

Note, this still means you can only use callbacks implemented in C,
but assuming I've understood correctly and ut_write_to_stderr is an
actual function in the library, that might be all you need.

Martin

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.