gmlt.A
gmlt.A

Reputation: 357

Passing long value to Java with JNI

I'm trying to pass object pointer as long from C++ code to java method through JNI to convert it back to pointer in callback later.

void Client::process()
{
    long thisAddress = (long)this;
    QAndroidJniObject res = activity.callObjectMethod("process", "(Ljava/lang/Long;)Ljava/lang/String;", (jlong)thisAddress);
}

Java function prototype is public String process(Long clientAddr) and here is beauty that JVM prints to me: Invalid indirect reference 0x5f3d9bc8 in decodeIndirectRef.

What is wrong with this code? Or, maybe, there is another method to do what I want?

Upvotes: 2

Views: 4506

Answers (2)

Nejat
Nejat

Reputation: 32665

The signature for a type of jlong is J. Also note that a C++ long usually has 32 bits, so it is equivalent to a jint and there is no need to use a jlong here. But you can also assign a long to a jlong. It would be converted automatically.

So the method call should be like:

QAndroidJniObject res = activity.callObjectMethod("process", "(J)Ljava/lang/String;", thisAddress);

Upvotes: 2

gmlt.A
gmlt.A

Reputation: 357

Ok. I managed to fix my problem by replacing Float to float using short signature "J" instead, as @Nejat suggested.

So now it looks like that Qt:

QAndroidJniObject res = activity.callObjectMethod("process", "(J)Ljava/lang/String;", (jlong)thisAddress);

Java:

public String process(long clientAddr)

Upvotes: 2

Related Questions