Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK passing long values to native method

This is my function:

Long Java_my_package_MainActivity_getDistance(JNIEnv* env, jobject obj, Long a_id1,Long a_id2)
    {
        char temp[128];
        sprintf(temp,"ID1: %u , ID2: %u",a_id1, a_id2);
        __android_log_print(ANDROID_LOG_INFO, "From NDK : ", temp);
        return(0);//just to test ... 
    }

Long is a type def:

typedef long long Long;

because i have some values that are grater than the max value of int type

in my java class:

 static {
            System.loadLibrary("module"); 
        }
        // declare the native code function - must match ndkfoo.c
    private native int getDistance(long id1, long id2);

I call the native function with this line:

getDistance(1234,2456);

the output in the logcat is:

ID1: 1234, ID2: 0

I always get ID2 = 0 !!, i don't know why the second parameter always evaluate to 0 !

like image 802
user2340271 Avatar asked Mar 24 '26 08:03

user2340271


1 Answers

Or try that:

#include <jni.h>
#include <android/log.h>

#define TAG "Java_my_package_MainActivity"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)

// ...

JNIEXPORT void JNICALL Java_my_package_MainActivity_getDistance(JNIEnv *env, jobject obj, jlong a_id1, jlong a_id2){

    // ...

    LOGI("ID1: %lu , ID2: %lu", a_id1, a_id2);
}
like image 163
Darius Avatar answered Mar 25 '26 22:03

Darius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!