I am trying to convert a boolean array being passed to C++ from Java into just a bool array. However, regardless of what the boolean value is, it is always evaluated to true because the value is a number greater than 0. My latest attempt code is:
jint capsLen = env->GetArrayLength(capabilities);
if (capsLen <= 0)
{
print error;
return;
}
bool capsArray[capsLen];
jboolean *getbool = env->GetBooleanArrayElements(capabilities, NULL);
for (int i = 0; i < capsLen; i++)
capsArray[i] = (bool)getbool[i]
I have also tried:
jboolean getbool[1];
bool capsArray[capsLen];
for (int i = 0; i < capsLen; i++)
{
env->GetBooleanArrayRegion(capabilities, i, 1, getbool);
capsArray[i] = getbool;
}
Both bits of code gives the getbool variable an integer value greater than 1.
I am not quite sure what I am doing wrong, and cannot seem to find any example code online that works (that is if I can find anything at all). How am I supposed to rip boolean values from a jbooleanArray passed from Java into a bool array in C++?
Also, in case someone suggests it, I cannot use an integer array instead of a boolean array because the Java code HAS to have the information as booleans.
EDIT: @sigpwned For the first code block, an example of the returned values (using printf("%d", getbool[i])):
208, 160, 155, 65, 248, 106, 154, 65, 248, 106, 154, 65
It should be noted that these numbers are not always consistent.
EDIT: The provided boolean values are:
F, T, T, F, F, F, T, F, T, F, T, T
EDIT: Full function code
JNIEXPORT bool JNICALL Java_com_NativeClient_Login(JNIEnv *env, jobject, jstring userJ, jstring passJ, jstring serJ, jboolean useJ, jstring resJ, jbooleanArray capabilities)
{
LOGI("%s", __PRETTY_FUNCTION__);
if (cst_ == NULL) {
LOGE("cst is not initialized");
return false;
}
std::string user = env->GetStringUTFChars(userJ, NULL);
std::string pass = env->GetStringUTFChars(passJ, NULL);
std::string ser = env->GetStringUTFChars(serJ, NULL);
std::string res = env->GetStringUTFChars(resJ, NULL);
jint capsLen = env->GetArrayLength(capabilities);
if (capsLen <= 0) {
LOGE("Provided capabilities array is empty or negative or errored");
return false;
}
bool capsArray[capsLen];
jboolean *getbool = env->GetBooleanArrayElements(capabilities, NULL);
LOGE("sizeof(getbool): %d", sizeof(getbool)); // Returns: 4
for (int i = 0; i < capsLen; i++) {
capsArray[i] = (getbool[i] == JNI_TRUE);
LOGE("capsArray[%d]: %d", i, capsArray[i]); // All zero
capsArray[i] = !(getbool[i] == JNI_FALSE);
LOGE("capsArray[%d]: %d", i, capsArray[i]); // All one
}
if (!cst_->SetCapabilitiesArray(capsArray, capsLen)) {
LOGE("Error is setting capabilities array, most likely it is a length mismatch");
return false;
}
cst_->SetUserInfo(user, pass, ser, useJ, res);
return true;
}
So the problem was not with the C++ side of code, but my lack of knowledge of Java and stubbornness to ask the Java people for help.
It turns out that I was building and passing my function a Boolean caps[] instead of a boolean caps[].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With