[英]I have 2 very similar C functions being called from python and Java. How can I combine the 2 libraries into 1 that can be called from both languages?
Basically I have 2 implementations of a C function "encrypt" which I call from python using ctypes and java using JNI. 基本上,我有2个C函数“ encrypt”的实现,我使用ctypes从python调用它,而使用JNI从java调用。 I've been told to take the two dynamic libraries that were used for java and python, and combine them into a static library that can be called from both.
我被告知要使用用于Java和python的两个动态库,并将它们组合成一个可以从两者中调用的静态库。 To clarify, here's the C function implementation for java using JNI:
为了澄清,这是使用JNI的Java C函数实现:
#include "jniTest.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_jniTest_passBytes
(JNIEnv *env, jclass cls, jbyteArray array) {
unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
jsize size = (*env)->GetArrayLength(env,array);
for(int i=0; i<size; i++){
buffer[i] += 1;
printf("%c",buffer[i]);
}
(*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}
So the function takes in a byte array and increments each byte by 1 then returns each element of the new byte array. 因此,该函数接收一个字节数组,并将每个字节加1,然后返回新字节数组的每个元素。 Here's the java side:
这是Java方面:
class jniTest{
public static native void passBytes(byte[] bytes);
static{
System.loadLibrary("encrypter");
{
public static void main(String[] args){
Tester tester = new Tester();
byte[] b = "hello";
tester.passBytes(b);
}
}
The python implementation of the C function is the exact same, just not using all that JNI syntax: C函数的python实现完全相同,只是不使用所有JNI语法:
#include <stdio.h>
void encrypt(int size, unsigned char *buffer);
void encrypt(int size, unsigned char *buffer){
for(int i=0; i < size; i++){
buffer[i]+=1;
printf("%c", buffer[i]);
}
}
And the python side: 和python方面:
import ctypes
encryptPy = ctypes.CDLL('/home/aradhak/Documents/libencrypt.so')
hello = "hello"
byteArr = bytearray(hello)
rawBytes = (ctyes_c.ubyte*len(byteArr)(*(byteArr))
encryptPy.encrypt(len(byteArr), rawBytes)
As you can see, the C function for python and java do the exact same thing (few minor differences). 如您所见,用于python和java的C函数做的事情完全相同(差异不大)。 They take in a byte array, increment each byte by 1, and print each incremented byte.
它们采用字节数组,将每个字节加1,然后打印每个递增的字节。 I just need to combine these two C libraries and combine them into a single static library which can be accessed from both python AND java.
我只需要组合这两个C库并将它们组合成一个可以从python和java进行访问的静态库。 Is this possible?
这可能吗? Thanks.
谢谢。
You can call the python version from the java one. 您可以从Java版本中调用python版本。 This avoids the duplicate code.
这样可以避免重复的代码。 You'll still have to do the to/from JNI marshalling though:
不过,您仍然必须执行往返JNI编组的操作:
void encrypt(int size, unsigned char *buffer){
for(int i=0; i < size; i++){
buffer[i]+=1;
printf("%c", buffer[i]);
}
}
JNIEXPORT void JNICALL Java_jniTest_passBytes
(JNIEnv *env, jclass cls, jbyteArray array) {
unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
jsize size = (*env)->GetArrayLength(env,array);
encrypt(size, buffer);
(*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.