为什么C函数不能将正确的参数传递给java?

Sug*_*uge 0 c java java-native-interface android

在我的测试中,我想从C调用Java并传递一个long类型参数,当我使用long类型时,在Java方法中,传入的参数无法正确获取,它始终是4294967297.但是当我尝试使用int类型时一切都很好.有谁知道什么是错的?

JAVA:

public static void test(long num) {
    Log.d("test", "xxxxxxxxxxx:%ld" + String.valueOf(num));
}
Run Code Online (Sandbox Code Playgroud)

C:

void test_jni()
{
    long num = 5000;
    jclass theClass = (*currentJNIEnv)->FindClass(currentJNIEnv, "me/example/something/TestClass");
    if (NULL != theClass) {
        jmethodID mid = (*currentJNIEnv)->GetStaticMethodID(currentJNIEnv, theClass, "test", "(J)V");
        if (mid == 0) return;
        (*currentJNIEnv)->CallStaticVoidMethod(currentJNIEnv, theClass, mid, num);
    }
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*odo 6

long在Java中始终是64位整数类型,long在C中可能是32位整数类型(至少在x86 32位linux上).所以他们是不同的.这就是你得到垃圾的原因.

在C中,使用jlong而不是long,typedefed为足够长的整数数据类型.