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

  • Home
  • SEARCH
  • 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 3489858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:26:45+00:00 2026-05-18T11:26:45+00:00

I am trying to implement some C code in Java by using SWIG 1.3.

  • 0

I am trying to implement some C code in Java by using SWIG 1.3. Now I
have to rebuild some existing C into Java code and to provide a function
pointer to a Java function to the C method.

The C code:
net.c:

void register_message_handler( context_t *ctx, message_handler_t handler) {
context->msg_handler = (void (*)( void *, coap_queue_t *, void *)) handler;
}

client.c:

void message_handler(context_t  *ctx, queue_t *node, void *data) {
...
}

int main(int argc, char **argv) {
// setup ctx
register_message_handler( ctx, message_handler );
}

All I already have in Java is:

public static void message_handler(def.SWIGTYPE_p_context_t ctx, def.SWIGTYPE_p_queue_t node, String data ) {}

and this should be registered as callback in the same way as it is done in the
above C code, now in Java:

net.register_message_handler(ctx, message_handler);

What I found was
http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIG_nn30 including an
undefined reference at the end of this chapter:
“And now, a final note about function pointer support. Although SWIG does not
normally allow callback functions to be written in the target language, this
can be accomplished with the use of typemaps and other advanced SWIG features.
This is described in a later chapter.”
Where does this refer to?

I also found a solution for C++, but is there a way to adapt this to C?
Swig c++ w/ Java loses type on polymorphic callback functions
morphic-callback-functions

Thanks for your help.

  • 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-18T11:26:46+00:00Added an answer on May 18, 2026 at 11:26 am

    I remember scratching my head over this reference in the SWIG manual too.

    You can do this as follows without the esoteric features:

    • You need a mechanism to dispatch the incoming C callback into Java. For that you need the object ID of the object that you are calling into, and the method ID of your handler. In your C registration helper, create Global References for those and cache them for use by the callback.

    • You also need a class ID and constructor method ID for anything that you want to pass to the java callback as a parameter. You also want to cache Global References to those.

    • In the C part of the callback, look up your method IDs, construct arguments and call into Java.

    • The thread that the callback comes in on, needs to be attached to the Java VM (with the JNI function AttachCurrentThread()). This is where you get your JNIEnv pointer from. This pointer is only valid in the context of the thread that you invoked AttachCurrentThread() from! What this means is that if you have callbacks coming in on multiple threads, you need to cache the JNIEnv * in thread local storage.

    • Make sure you check return values after returning from JNI functions

    • Make sure to check ExceptionOccurred() after any and all calls back into Java. Not doing this really gets you in trouble in hard to debug ways.

    • I found this relatively easy to debug with Eclipse and Visual Studio as follows: Start main Java program from Eclipse, attach Visual Studio Debugger to that process. You can set breakpoints on either side.

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

Sidebar

Related Questions

I've been trying to implement unit testing and currently have some code that does
I have some code that performs a deep copy using Object.clone, but I'm trying
I am relatively new to Java and while trying out some code came across
I have some Java code which performs bitwise operations on a BitSet. I have
I'm trying to implement some retry logic if there is an exception in my
I'm trying to implement some basic linear algebra operations and one of these operations
I'm trying to implement the WMD editor used on StackOverflow to create some basic
Here are the specs that I'm trying to implement in a nutshell: 1) Some
trying to implement a dialog-box style behaviour using a separate div section with all
I'm a Java developer who's trying to move into C#, and I'm trying to

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.