Note that there are some explanatory texts on larger screens.

plurals
  1. POjni callback works for java types, but not c types
    text
    copied!<p>I have followed the advice at <a href="https://stackoverflow.com/questions/4313004/registering-java-function-as-a-callback-in-c-function">registering java function as a callback in C function</a> and can callback with "simple" types such as integer and string, e.g.:</p> <pre><code>jstring js = (*env)-&gt;NewStringUTF(env, "hello"); (*env)-&gt;CallStaticVoidMethod(env, cls, methodid, js); </code></pre> <p>However, if I am trying to do the same with C datatypes which have been wrapped with SWIG, I am only getting null pointers in Java. In the C part they are definitely not 0. Do they need to be treated differently?</p> <p>[EDIT:] Some more information:</p> <p>As stated above, char*/string is working for me as well. I am looking for a solution for C struct's, which have been wrapped by SWIG and have been allocated in Java.</p> <p>E.g.:</p> <pre><code>typedef struct { unsigned short length; unsigned short value; } lv_t; </code></pre> <p>is getting wrapped by SWIG, so I can use it in Java:</p> <pre><code>lv_t lv; lv = modulename.modulename_new_lv(); lv.setLength(1); lv.setValue(2); </code></pre> <p>Then I will give this struct from Java to C:</p> <pre><code>modulename.send(lv); </code></pre> <p>C will send it over the network, receive some reply and change the values in lv. Now, this should give the modified lv back to Java.</p> <pre><code>void jni_call_received_hook(lv_t* lv){ JNIEnv* m_env; (*m_vm)-&gt;AttachCurrentThread(m_vm, (void**) &amp;m_env, NULL ); jclass cls = (*m_env)-&gt;FindClass( m_env, "gui/StateMachine" ); jmethodID mid = (*m_env)-&gt;GetStaticMethodID(m_env, cls, "callReceivedEvent", "(Lcom/something/modulename/jni/lv_t;)V"); if (mid == 0){ log(E, "Unable to find method for callback"); return; } // what to do here to create a jobject? jobject lv_j = ...; (*m_env)-&gt;CallStaticVoidMethod(m_env, cls, mid, lv_j); </code></pre> <p>}</p> <p>Which calls:</p> <pre><code>public static void messageHandler(lv_t lv) { System.out.println("messageHandler()."); System.out.println("lv " + lv); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload