gpt4 book ai didi

java - 使用JNA将数组从java传递到dll函数

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:29 27 4
gpt4 key购买 nike

我想将 Java 数组作为参数传递给 c dll 抛出 JNA,这是我的代码:

import com.sun.jna.*;

public class Javatest {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
"test", CLibrary.class);
void test(Pointer p,int width);
}

public static void main(String[] args) {
Pointer p = new Memory(5*Native.getNativeSize(Double.TYPE));
for (int i = 0; i < 5; i++) {
p.setDouble(i*Native.getNativeSize(Double.TYPE),5);
}
CLibrary.INSTANCE.test(p,5);
}
}

C 代码:

#include <stdio.h>
__declspec(dllexport) int test(double *a,int width){
for(int i =0 ; i<width;i++){
printf("%d",a[i]);

}
return 0;
}

结果:00000

看起来指针指向正确的内存位置。

最佳答案

printf 格式有问题:%d 用于整数。请尝试使用 %f

关于java - 使用JNA将数组从java传递到dll函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24092495/

27 4 0
文章推荐: python - 使用 vObject (Python) 为 Outlook 创建 "downloadable"vCalendar 对象
文章推荐: html - 为交替的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com