简体   繁体   English

将布尔数组从C返回到Java

[英]Returning a boolean array from C to Java

I am trying to return an array of booleans from c to java via JNI 我正在尝试通过JNI将c的布尔数组返回到java

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. [警告]从不兼容的指针类型传递'(* env)-> SetBooleanArrayRegion'的参数5。

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. 最后一个参数应该是“ const jboolean * buf”,但我希望能够将常规布尔数组复制到jboolean数组。

And I keep getting this error 而且我不断收到这个错误

[Warning] passing argument 5 of '(*env)->SetBooleanArrayRegion' from incompatible pointer type. [警告]从不兼容的指针类型传递'(* env)-> SetBooleanArrayRegion'的参数5。

The last argument is supposed to be "const jboolean *buf", 最后一个参数应该是“ 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. 警告告诉您正在传递的数组的元素类型booljboolean不兼容,因为这两种类型恰好在您的环境中定义。 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. 但我希望能够将常规布尔数组复制到jboolean数组。

But that's not what you're doing. 但这不是你在做什么。 You are trying to initialize a jbooleanarray (not " jboolean array") from a bool array. 您正在尝试从bool数组初始化jbooleanarray (不是“ jboolean array”)。 To the extent that bool and jboolean are indeed incompatible, that's not possible via any available JNI function. 在一定程度上booljboolean实际上是不兼容的,这不可能通过任何可用的JNI函数来实现。 But you can set values in a jbooleanarray from a jboolean array, like so: 但是您可以通过jboolean数组在jbooleanarray设置值,如下所示:

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数组,而是从某个外部源获取bool数组,那么就不能只切换到jboolean ,那么最好的选择可能是通过GetBooleanArrayElementsjbooleanarray获取一个jboolean数组,并通过适当的循环复制值,然后完成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. 或者,使用Get / ReleaseBooleanArrayRegionGet / ReleaseBooleanArrayCritical来最好地满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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