简体   繁体   中英

Returning a boolean array from C to Java

I am trying to return an array of booleans from c to java via JNI

JNIEXPORT jbooleanArray JNICALL Java_NetworkGUI_passwordProtected(JNIEnv 
*env, jobject obj)
{
    bool passwordProtected[3];

    passwordProtected = {true, false, false} 
    jbooleanArray passwords;

    passwords = (*env) -> NewBooleanArray(env, 3); 

    (*env)-> SetBooleanArrayRegion(env, passwords, 0, 3, 
     passwordProtected);

    return (passwords);
}

And I keep getting this error

[Warning] passing argument 5 of '(*env)->SetBooleanArrayRegion' from incompatible pointer type.

The last argument is supposed to be "const jboolean *buf", but I want to be able to copy a regular bool array to a jboolean array.

And I keep getting this error

[Warning] passing argument 5 of '(*env)->SetBooleanArrayRegion' from incompatible pointer type.

The last argument is supposed to be "const jboolean *buf",

The warning is telling you that that the the element type of the array you are passing, bool , is not a type compatible with jboolean , as those two types happen to be defined in your environment. This is not a problem you can ignore, at least not without more details. There is no reason to expect that the function you are calling will work as expected when you pass an argument that is mismatched in this way.

but I want to be able to copy a regular bool array to a jboolean array.

But that's not what you're doing. You are trying to initialize a jbooleanarray (not " jboolean array") from a bool array. To the extent that bool and jboolean are indeed incompatible, that's not possible via any available JNI function. But you can set values in a jbooleanarray from a jboolean array, like so:

jboolean passwordProtected[] = {JNI_TRUE, JNI_FALSE, JNI_FALSE};
jbooleanArray passwords;

passwords = (*env)->NewBooleanArray(env, 3); 

(*env)-> SetBooleanArrayRegion(env, passwords, 0, 3, passwordProtected);

If instead of declaring one locally, you get your bool array from some external source, so that you cannot just switch to jboolean , then your best alternative is probably to obtain a jboolean array from the jbooleanarray via GetBooleanArrayElements , copy the values via an appropriate loop, and then finish up with ReleaseBooleanArrayElements :

bool passwordProtected[] = {true, false, false};
jbooleanArray passwords = (*env)->NewBooleanArray(env, 3);
jboolean *password_elements = (*env)->GetBooleanArrayElements(env, passwords, NULL); 

for (int i = 0; i < 3; i++) {
    password_elements[i] = passwordProtected[i] ? JNI_TRUE : JNI_FALSE;
}

(*env)->ReleaseBooleanArrayElements(env, passwords, password_elements, 0);

Alternatively, use Get / ReleaseBooleanArrayRegion or Get / ReleaseBooleanArrayCritical , as best matches your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM