Reference String passing (JNI to Java)

3,075 views
Skip to first unread message

Riasat Abir

unread,
Apr 11, 2011, 2:50:30 AM4/11/11
to android-ndk
Hi,

I am facing a problem. I want to pass the value of string from jni to java part. How to do it?

Say my native function is: int myFunction(string sBuff) which returns the length of sAddress. Initially I pass empty string sAddress from Java which gets a value there.

So the native function should be like this:

jint Java_com_example_myTestActivity_myFunc(JNIEnv* env, jobject thiz, jstring sBuff){

    // Using some other function call, I will get the value of the string and its length. I want to pass it through sBuff.

    char * dest;
    int iLen = af_recv(dest,x,y,z);

   // Now I've got the string value in dest and length in iLen, I want to pass the value of dest in sBuff. How to do it??

   return iLen;
}

Riasat Abir

unread,
Apr 11, 2011, 6:04:04 AM4/11/11
to android-ndk
I tried with kind of thing:


jint Java_com_example_myTestActivity_myFunc(JNIEnv* env, jobject thiz, jstring sBuff){

    // Using some other function call, I will get the value of the string and its length. I want to pass it through sBuff.

    std::string tmp("test");
    const char * dest;
    dest = tmp.c_str();
    sBuff= (env)->NewStringUTF(dest); // tried to put the the value of dest in sBuff didn't work

   
   // Now I've got the string value in dest and length in iLen, I want to pass the value of dest in sBuff. How to do it??

   return iLen;
}

So anyone can tell its possible or not?
Passing the value of string through parameter instead of returning?

Thanks,
Riasat
--

HimHim

Riasat Abir

unread,
Apr 11, 2011, 8:56:46 AM4/11/11
to android-ndk
Anyone has any idea about it?
Or I've to send structure containing the String.

I don't know how to send structure from JNI to Java also, need some help :(

David/Alan can you please check it?

Thanks
Riasat
--

HimHim

Philippe Simons

unread,
Apr 11, 2011, 9:25:52 AM4/11/11
to andro...@googlegroups.com, Riasat Abir
in Java, String are immutable, you cant modify sBuff

you need to return a new String

If you want to return the String and the length (maybe you could just ask in your java side the length of the returned String), you need to create a Bean Class, instantiate it in your native function and populate it before returning it to Java

public MyBeanClass
{
  public String mString;
  public int mSize;
}

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Riasat Abir

unread,
Apr 11, 2011, 11:17:22 AM4/11/11
to Philippe Simons, android-ndk
Thanks for your reply Simons :)

I thought that also. Yes I can return the string and check the length from java but that will also problem in many cases because in my C/C++ functions many string value were passed in the reference parameter.

If I want to pass the class object to java what will be the return type? How would Java know this object its not defined there.

I've not returned a class object before in  the ndk. So can anyone give a simple example of it?

Thanks,
Riasat
--

HimHim

Philippe Simons

unread,
Apr 11, 2011, 11:34:04 AM4/11/11
to Riasat Abir, android-ndk
the Java class must defined in a standard .java file

return type of your jni function will be a jobject.

look at JNI documentation to find out how to instantiate a new java object and access the instance fields of it (google for it)

alan

unread,
Apr 12, 2011, 4:59:38 AM4/12/11
to andro...@googlegroups.com, Riasat Abir
the easiest way to implement passing parameters by reference in java is to use a single element array, you can then change the value held in the array

Riasat Abir

unread,
Apr 12, 2011, 11:55:54 PM4/12/11
to andro...@googlegroups.com
Yes changing the value is possible in Java but what about from the jni string to Java string?
If you want to pass an empty string from java and put some value in it from jni native functions and retrieve the value in java again?


On Tue, Apr 12, 2011 at 2:59 PM, alan <al...@birtles.org.uk> wrote:
the easiest way to implement passing parameters by reference in java is to use a single element array, you can then change the value held in the array



--

HimHim

alan

unread,
Apr 13, 2011, 4:23:10 AM4/13/11
to andro...@googlegroups.com
then you put the empty string into the first element of the array, pass the array to the native code, use jni functions to read/write to the array. back in java you can then retrieve the element from the array and it should be your new string

Riasat Abir

unread,
Apr 13, 2011, 4:32:53 AM4/13/11
to andro...@googlegroups.com
Yes I tried this way.
Passed empty String (sBuff) from java. Now in JNI functions I tried to put something to sBuff. Didn't work.


jint Java_com_example_myTestActivity_myFunc(JNIEnv* env, jobject thiz, jstring sBuff){

    // Using some other function call, I will get the value of the string and its length. I want to pass it through sBuff.

    std::string tmp("test");
    const char * dest;
    dest = tmp.c_str();
    sBuff= (env)->NewStringUTF(dest); // tried to put the the value of dest in sBuff didn't work

   
   // Now I've got the string value in dest and length in iLen, I want to pass the value of dest in sBuff. How to do it??

   return iLen;
}


On Wed, Apr 13, 2011 at 2:23 PM, alan <al...@birtles.org.uk> wrote:
then you put the empty string into the first element of the array, pass the array to the native code, use jni functions to read/write to the array. back in java you can then retrieve the element from the array and it should be your new string

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

alan

unread,
Apr 13, 2011, 5:41:45 AM4/13/11
to andro...@googlegroups.com
your java function should be:
native int myFunc(string[] buff);

then your native function would be:
jint Java_com_example_myTestActivity_myFunc(JNIEnv* env, jobject thiz, jarray sBuff){

Riasat Abir

unread,
Apr 18, 2011, 6:28:55 AM4/18/11
to andro...@googlegroups.com
Hi Alan,

I tried using jarray but I could not do it right :(

Can you please show how to pass something in buff using jarray?

jint Java_com_example_myTestActivity_myFunc(JNIEnv* env, jobject thiz, jarray buff){

    // how to pass a character array to jarray buff? that would be read from the java functions.
    return 0;
}

Regards,
Riasat

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.



--

HimHim

Zoran Angelov

unread,
Apr 18, 2011, 10:28:55 AM4/18/11
to andro...@googlegroups.com
Hi,
you cannot resize the array but you can modify its content.
So make sure that the array is big enough to store your data.
You can modify (put data in array buffer) with something like this:

jboolean isCopy=JNI_FALSE; //it depends on VM but usualy dalvik does not copy the data,  it actually gives you direct access to jbyteArray buffer
jbyte *data=env->GetByteArrayElements(buff,&isCopy); //dalvik will assign JNI_FALSE to isCopy

//here modify data but make sure it is bounded to env->GetArrayLength(buff);

//and finaly release the elements
env->ReleaseByteArrayElements(buff, data, 0); //pass 0 for mode, dalvik will handle the data buffer pointer
Reply all
Reply to author
Forward
0 new messages