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 8486601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:59:25+00:00 2026-06-10T20:59:25+00:00

My C program uses callback functions which are periodically called. I want to be

  • 0

My C program uses callback functions which are periodically called. I want to be able to handle the callback functions in a Java or C# program. How should I write the .i file to achieve this?

The C callback looks so:

static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata)
  • 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-06-10T20:59:27+00:00Added an answer on June 10, 2026 at 8:59 pm

    You can do this if you have a chance to pass some data around with the callback, but you’ll need to write some JNI glue. I put together a complete example of how you might map C style callbacks onto a Java interface.

    The first thing you need to do is decide on an interface that’s appropriate on the Java side. I assumed in C we had callbacks like:

    typedef void (*callback_t)(int arg, void *userdata);
    

    I decided to represent that in Java as:

    public interface Callback {
      public void handle(int value);
    }
    

    (The loss of the void *userdata on the Java side isn’t a real problem since we can store state in the Object that implements Callback trivially).

    I then wrote the following header file (it shouldn’t really be just a header, but it keeps things simple) to exercise the wrapping:

    typedef void (*callback_t)(int arg, void *data);
    
    static void *data = NULL;
    static callback_t active = NULL;
    
    static void set(callback_t cb, void *userdata) {
      active = cb;
      data = userdata;
    }
    
    static void dispatch(int val) {
      active(val, data);
    }
    

    I was able to successfully wrap this C with the following interface:

    %module test
    
    %{
    #include <assert.h>
    #include "test.h"
    
    // 1:
    struct callback_data {
      JNIEnv *env;
      jobject obj;
    };
    
    // 2:
    void java_callback(int arg, void *ptr) {
      struct callback_data *data = ptr;
      const jclass callbackInterfaceClass = (*data->env)->FindClass(data->env, "Callback");
      assert(callbackInterfaceClass);
      const jmethodID meth = (*data->env)->GetMethodID(data->env, callbackInterfaceClass, "handle", "(I)V");
      assert(meth);
      (*data->env)->CallVoidMethod(data->env, data->obj, meth, (jint)arg);
    }
    %}
    
    // 3:
    %typemap(jstype) callback_t cb "Callback";
    %typemap(jtype) callback_t cb "Callback";
    %typemap(jni) callback_t cb "jobject";
    %typemap(javain) callback_t cb "$javainput";
    // 4:
    %typemap(in,numinputs=1) (callback_t cb, void *userdata) {
      struct callback_data *data = malloc(sizeof *data);
      data->env = jenv;
      data->obj = JCALL1(NewGlobalRef, jenv, $input);
      JCALL1(DeleteLocalRef, jenv, $input);
      $1 = java_callback;
      $2 = data;
    }
    
    %include "test.h"
    

    The interface has quite a few parts to it:

    1. A struct to store the information needed to make a call to the Java interface.
    2. An implementation of the callback_t. It accepts as user data the struct we just defined and then dispatches a call to the Java interface using some standard JNI.
    3. Some typemaps that cause the Callback objects to be passed straight to the C implementation as a real jobject.
    4. A typemap that hides the void* on the Java side and sets up a callback data and fills in the corresponding arguments for the real function to use the function we just wrote for dispatching calls back to Java. It takes a global reference to the Java object to prevent it from being garbage collected subsequently.

    I wrote a little Java class to test it with:

    public class run implements Callback {
      public void handle(int val) {
        System.out.println("Java callback - " + val);
      }
    
      public static void main(String argv[]) {
        run r = new run();
    
        System.loadLibrary("test");
        test.set(r);
        test.dispatch(666);    
      }
    }
    

    which worked as you’d hope.

    Some points to note:

    1. If you call set multiple times it will leak the global reference. You either need to supply a way for the callback to be unset, prevent setting multiple times, or use weak references instead.
    2. If you have multiple threads around you will need to be smarter with the JNIEnv than I’ve been here.
    3. If you wanted to mix Java and C implementations of callbacks then you’ll need to expand on this solution quite a bit. You can expose C functions to be used as callbacks with %constant but these typemaps will prevent your wrapped functions from accepting such inputs. Probably you would want to supply overloads to work around that.

    There’s some more good advice in this question.

    I believe that a solution for C# would be somewhat similar, with different typemap names and a differing implementation of the callback function you write in C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program uses a NetworkOutput object which can be used to write data to
I want to embed a dictionary.txt which my program uses a streamreader object to
I'm writing a DLL which a java program uses to call the WinAPI in
How do I check how much resources a java program uses? In java it
I'm developing a Java plugin for an existing Java program. The existing program uses
My program uses MVC. When the user hits New File, I need to check
I have a simple program that uses FindWindowEx & strncmp() inside a callback passed
There is a program and dll which the program uses (both are written on
I'm trying to use .Net serialport methods from another program which uses Unity3D engine.
I have a program which uses an io_service and several threads. It instantiates some

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.