title: registering java function as a callback in C function
body: <p>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.</p>
  
  <p>The C code:
  net.c:</p>
  
  <pre><code>void register_message_handler( context_t *ctx, message_handler_t handler) {
  context-&gt;msg_handler = (void (*)( void *, coap_queue_t *, void *)) handler;
  }
  </code></pre>
  
  <p>client.c:</p>
  
  <pre><code>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 );
  }
  </code></pre>
  
  <p>All I already have in Java is:</p>
  
  <pre><code>public static void message_handler(def.SWIGTYPE_p_context_t ctx, def.SWIGTYPE_p_queue_t node, String data ) {}
  </code></pre>
  
  <p>and this should be registered as callback in the same way as it is done in the 
  above C code, now in Java:</p>
  
  <pre><code>net.register_message_handler(ctx, message_handler);
  </code></pre>
  
  <p>What I found was
  <a href="http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIG_nn30" rel="nofollow noreferrer">http://www.swig.org/Doc1.3/SWIGDocumentation.html#SWIG_nn30</a> 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?</p>
  
  <p>I also found a solution for C++, but is there a way to adapt this to C?
  <a href="https://stackoverflow.com/questions/1077387/swig-c-w-java-loses-type-on-poly">Swig c++ w/ Java loses type on polymorphic callback functions</a>
  morphic-callback-functions</p>
  
  <p>Thanks for your help.</p>