简体   繁体   中英

JNI: passing bytes from c++ to java

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);

problem is that byte array contains just null bytes in java side, does anyone have idea why?

EDIT: oh my bad it should be SetByteArrayRegion, sorry: all working now :)

I think your missing a line like:

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

check out:

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

for more details

also a similar question was answered here

How to return an array from JNI to Java?

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