Search Results

Search found 4 results on 1 pages for 'lhw'.

Page 1/1 | 1 

  • JNI loses reference to native methods

    - by lhw
    As an example for later use in Android I wrote a simple callback interface. While doing so i ran into the following error or bug or whatever. In C the two commented lines are supposed to be executed resulting in calling the C callback onChange. But instead i get an UnsatisfiedLinkError. Calling the native Method directly in Java works just fine. Calling it directly from C as presented here in the example also produces the UnsatisfiedLinkError. I'm open for any advice concerning this issue or work arounds and so on. The Java Part: import java.util.LinkedList; import java.util.Random; interface Listener { public void onChange(float f); } class Provider { LinkedList<Listener> all; public Provider() { all = new LinkedList<Listener>(); } public void registerChange(Listener lst) { all.add(lst); } public void sendMsg() { Random rnd = new Random(); for(Listener l : all) { try { l.onChange(rnd.nextFloat()); } catch(Exception e) { System.out.println(e); } } } } class Inheritance implements Listener { static public void main(String[] args) { System.load(System.getProperty("user.dir") + "/libinheritance.so"); } public native void onChange(float f); } The C Part: #include "inheritance.h" jint JNI_OnLoad(JavaVM *jvm, void *reserved) { JNIEnv *env; (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4); inheritance = (*env)->FindClass(env, "Inheritance"); o_inheritance = (*env)->NewObject(env, inheritance, (*env)->GetMethodID(env, inheritance, "<init>", "()V")); provider = (*env)->FindClass(env, "Provider"); o_provider = (*env)->NewObject(env, provider, (*env)->GetMethodID(env, provider, "<init>", "()V")); (*env)->CallVoidMethod(env, o_inheritance, (*env)->GetMethodID(env, inheritance, "onChange", "(F)V"), 1.0); //(*env)->CallVoidMethod(env, o_provider, (*env)->GetMethodID(env, provider, "registerChange", "(LListener;)V"), o_inheritance); //(*env)->CallVoidMethod(env, o_provider, (*env)->GetMethodID(env, provider, "sendMsg", "()V")); (*env)->DeleteLocalRef(env, o_inheritance); (*env)->DeleteLocalRef(env, o_provider); return JNI_VERSION_1_4; } JNIEXPORT void JNICALL Java_Inheritance_onChange(JNIEnv *env, jobject self, jfloat f) { printf("[C] %f\n", f); } The header file: #include <jni.h> /* Header for class Inheritance */ #ifndef _Included_Inheritance #define _Included_Inheritance #ifdef __cplusplus extern "C" { #endif jclass inheritance, provider; jobject o_inheritance, o_provider; /* * Class: Inheritance * Method: onChange * Signature: (F)V */ JNIEXPORT void JNICALL Java_Inheritance_onChange(JNIEnv *, jobject, jfloat); jint JNI_OnLoad(JavaVM *, void *); #ifdef __cplusplus } #endif #endif Compilation: gcc -c -fPIC -I /usr/lib/jvm/java-6-openjdk/include -I /usr/lib/jvm/java-6-openjdk/include/linux/inheritance.c inheritance.h gcc -g -o -shared libinheritance.so -shared -Wl,-soname,libinheritance.so -lc inheritance.o

    Read the article

  • C question: error: expected ')' before '*' token

    - by lhw
    ===EDIT I apologize for not putting the pcb struct into the code snippet. There is a struct called pcb defined in above the two structs I originally posted. Namely, typedef struct{ UINT32 proc; struct pcb *link; }pcb; Hi, I asked a question regarding structs in C a few minutes ago and got an answer blazing fast. But now I'm facing another problem, namely the error in the title of this question. I'm trying to implement a simple priority queue in C using arrays of queues. However, when I try to declare a function on pcb_pQ structure, I get the above error. I have the structs clearly defined in the heard file. In the header file: typedef struct{ pcb *head; pcb *tail; SINT32 size; } pcb_Q; typedef struct pcb_pQ { pcb_Q queues[5]; SINT32 size; } pcb_pQ; Function prototype in header file: /*priority queue operations*/ VOID pcb_pq_enqueue(pcb_pQ*, pcb*); Function impelmentation in .c file: VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ, pcb* pcb) { pcb_Q* pcb_Q_p; int priority; priority = pcb->proc_priority; pcb_Q_p = &pcb_pQ->queues[priority]; pcb_enqueue(pcb_Q_p, pcb); } When I try to compile the above code, I get an "error: expected ')' before '*' token". This error is pointing to the function signature in the .c file, namely VOID pcb_pq_enqueue(pcb_pQ* pcb_pQ, pcb* pcb) { But I am not sure why I am getting this error, could someone give me a hand? Thanks a lot.

    Read the article

  • Using Interfaces in JNI

    - by lhw
    I am trying to use (*env)->RegisterNatives to add methods to a defined class which I then add to a callback list. The callback sender of course expects my class to implement a certain interface which I do not. And is failing on execution. If I add the keyword "implements Listener" to my class in Java the javac expects to have the methods definition in Java or with native keyword which I try to avoid here, as I'd like to register the methods within the JNI_OnLoad and execute one of them afterwards. The question now is: Can I implement the interface in JNI or avoid the error message in Java?

    Read the article

  • c struct question

    - by lhw
    Hi, I'm trying to implement a simple priority queue from array of queues. I'm trying to define a struct queue, and than a struct priority queue that has an array of queues as its member variable. However, when I try to compile the code, I get the following error: pcb.h:30: error: array type has incomplete element type The code is below: typedef struct{ pcb *head; pcb *tail; SINT32 size; } pcb_Q; typedef struct { struct pcb_Q queues[5]; SINT32 size; } pcb_pQ; Could someone give me a hand? Thanks a lot.

    Read the article

1