Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling java string into native C/C++ for Android Studio+NDK

I am trying to build a program using Android Studio1.3.2 + NDK + Windows7. I want to pass a string from Java function to Native C function (const char*).

The native C function declaration is:-

public native int HRFromJNI(String path);

The Java function call is:-

tv.setText(String.valueOf(HRFromJNI(path)));

The Native C function is:-

extern "C" {
  JNIEXPORT jint JNICALL
  Java_com_example_hellojni_HelloJni_HRFromJNI
  (JNIEnv *env, jobject obj,jstring path)
  {
    int HRval = 0;
    char *Path;

    Path = (*env)->GetStringUTFChars( env, path, null) ;

    HRval = filefunction(Path);
    return HRval;
  }

The function called by the Native C function is :-

int filefunction(char* filename)
{
FILE* file = fopen((char*)filename,"w+");
//Reads value from the file and returns it.
.
.
.
}

But i get error "Base operand of -> has non-pointer type JNIEnv{aka _JNIEnv}"

Is this the correct way of passing string to Native C code, or is there any other way to assign a string from Java to const char* ?

like image 613
Sandrocottus Avatar asked Jul 12 '26 09:07

Sandrocottus


1 Answers

Change below line:

Path = (*env)->GetStringUTFChars( env, path, null) ;

To:

Path = env->GetStringUTFChars( env, path, JNI_TRUE) ;

Pass jni boolean variable, instead of passing null.

like image 173
Saritha G Avatar answered Jul 13 '26 22:07

Saritha G