Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:59:18+00:00 2026-05-28T03:59:18+00:00

I am trying to write a wrapper around a C program so that I

  • 0

I am trying to write a wrapper around a C program so that I can call it from Python. I am using Cython to do this. The C function takes a callback function as an argument, but this call back function will only be known at run-time of the python program. I have been searching how to do this and it seems there is not simple solution but the following seems to work:

#python.py

libc = cdll.LoadLibrary("myfunc.so") #Callback function is defined in myfunc.so 
....
c_wrapper(libc.fun, ...)

.

#c_wrapper.pyx

cdef extern void mainfunction(void *F, ...) #The intial C function we are wrapping
ctypedef void (*myfuncptr) () 

def c_wrapper(f, ...) # our function pointer is passed to the wrapper as a Python object
    cdef myfuncptr thisfunc
    thisfunc = (<myfuncptr*><size_t>addressof(f))[0]
    mainfunction(thisfunc, ...)

This method works for C and FORTRAN functions (i am assuming it will work for most compiled languges) and Python functions (using C types), but it seems a little awkward. Is there any more simple way of doing this in Cython?

Thanks

EDIT : I am unable to change the C library I am trying to wrap

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:59:19+00:00Added an answer on May 28, 2026 at 3:59 am

    I guess you’re aware of this?

    Is it possible to call my Python code from C?

    Answer: Yes, easily. Follow the example in Demos/callback/ in the
    Cython source distribution

    So, knowing that you can dload your main function, let’s take another approach. I wrote a couple of stupid functions to test this:

    /* lib.c -> lib.so */
    #include <stdio.h>
    
    void fn1(void) {
            puts("Called function 1");
    }
    
    void fn2(void) {
            puts("Called function 2");
    }
    

    then, the function that takes the callback

    /* main.c -> main.so */
    
    typedef void (*callback)();
    
    void mainfunction(void *F) {
            ((callback)F)();
    }
    

    Which can be passed directly from Python:

    >>> from ctypes import cdll
    >>> lib = cdll.LoadLibrary('./lib.so')
    >>> main = cdll.LoadLibrary('./main.so')
    >>> main.mainfunction(lib.fn1)
    Called function 1
    >>> main.mainfunction(lib.fn2)
    Called function 2
    

    And now, let’s wrap a Python function:

    >>> from ctypes import CFUNCTYPE
    >>> def pyfn():
    ...     print "Called the Python function"
    ... 
    >>> CWRAPPER = CFUNCTYPE(None)
    
    >>> wrapped_py_func = CWRAPPER(pyfn)
    >>> main.mainfunction(wrapped_py_func)
    Called the Python function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple wrapper class in F# that takes a function
I am trying to write a simple wrapper around a connection pointer that will
I'm trying to write a Java function that takes a Class<?> and returns a
I'm trying to write some wrapper class or function that allows me to execute
I'm trying to write a wrapper function around expressjs's app.get The get (and other
I'm trying to write a wrapper around Image_Graph bar charts. I actually wrote the
I'm trying to write a COM++ object wrapper around a Qt widget (control) I
I have an OLE COM object that trying to write a wrapper for, I
I'm trying to write a SWIG wrapper for a C library that uses pointers
I'm currently trying to write a voicechat program in python. All tips/trick is welcome

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.