JNI:将字节从c ++传递给java

时间:2011-06-06 00:39:58

标签: java java-native-interface

HANDLE hFile = CreateFileA("C:\\myfile.zip", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
const int size = GetFileSize(hFile, NULL);
char* buffer = new char[size];
DWORD read;
ReadFile(hFile, buffer, size, &read, NULL);

jclass cls = ...;
jmethodID id = ...;

jbyteArray arr = env->NewByteArray(size);
env->GetByteArrayRegion(arr, 0, size, (jbyte*) buffer);
env->CallStaticVoidMethod(cls, id, arr);

问题是字节数​​组在java方面只包含空字节,有没有人知道为什么?

编辑:哦,我的不好应该是SetByteArrayRegion,对不起!现在都在工作:)

1 个答案:

答案 0 :(得分:11)

我想你错过了一句话:

(*env)-> SetByteArrayRegion(env, result, 0, size, fill);

退房:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html

了解更多详情

这里也回答了类似的问题

How to return an array from JNI to Java?