简体   繁体   中英

Pointer References Between C and Java

I am totally new to JNI and C and I am having problems getting my head wrapped around how to do this task.

I have a native C function with the following signature: void DoEncrypt(char *buffer, int *length) { ... encrypt data in buffer in place ... ... }

I also have the reverse method to do the decryption on the way back. The problem I am having is figuring out, with JNI, how to pass in the two pointers and then retrieve the encrypted or decrypted password, from its memory location, by either the wrapper class or the original Java method.

Right now, my thinking is to write a wrapper in C to call the DoEncrypt and DoDecrypt functions that will use jobject and jstring to get the password value in and out.

Is this correct? Is it the best / simplest / fastest way?

I appreciate all help you can share.

Thank you,

Chuck

the wrapper functions are good, since you can test the DoEncrypt and DoDecrypt directly from C. You can write the wrapper like this :

void DoEncrypt_jni(JNIEnv** env, jarray* buffer, int* length) {
    ... call the real DoEncrypt ...
}

(it's been a long time since I wrote a JNI function. It may be not so right...) In alternative, you can try to use JNA , which is basically "JNI with less pain". With JNA, you can write a Java class

public class EncryptUtilsJNA {
    public static native void DoEncrypt(byte[] buffer, int length);
    public static native void DoDencrypt(byte[] buffer, int length);
}

and JNA works out how to call the DoEncrypt/DoDecrypt C functions, which obviously they have to be callable from Java like you would do with JNI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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