gpt4 book ai didi

java - JNI : passing bytes from c++ to java

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:43 27 4
gpt4 key购买 nike

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,抱歉!现在一切正常:)

最佳答案

我认为你漏掉了一行:

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

checkout :

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

了解更多

这里也回答了类似的问题

How to return an array from JNI to Java?

关于java - JNI : passing bytes from c++ to java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6246915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com