2

How do I send a pointer to array from JNI C code to a Java code ? For example :

JNI C Code :

jclass cls = (*env)->GetObjectClass(env,obj);
jmethodID mid = (*env)->GetMethodID(env,cls,"print"," ?? "); // What should be the signature here ?
jvalue a1,*arr;
a1.i = 2002;
a1.f = 12.90;
a1.c = 's';
a1.j = 344554;
a1.b = TRUE;
arr = &a1;
(*env)->CallVoidMethodA(env,obj,mid,arr);

Java Func :

public void print(?????) { // what should be the argument here ?
  // add code here
}

How should the java function look like to receive a pointer to an array ?

2
  • 3
    @auselen leave this comment under all the questions on SO !(This homework is a preparatory work for some module I want to write) Next time try to give a better answer,so that you don't get 2 downvotes.. Commented Oct 6, 2012 at 7:21
  • Are there any special reasons to why you are using CallVoidMethodA() instead of the more easy CallVoidMethod()? Commented Oct 7, 2012 at 18:28

1 Answer 1

1

First off, two problems with your code:

  1. arr is not actually an array (it points to a single jvalue), so therefore there's no reason to pass it by pointer instead of just by value.
  2. All those a1.X assignments are useless, except the last one, because jvalue is a union of all the possible primitive and reference types.

Next: It's impossible to pass raw C pointers to JNI (well, you can, but you can only get Java to see the pointer as a number and not as an object). You have to create a Java array or buffer object. Arrays are good if you will be creating an array of references, though the memory will have to be copied; buffers are good if you want the C array to share memory with the Java buffer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.