繁体   English   中英

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

[英]Passing array from java to dll function with JNA

我想将Java数组作为参数传递给c dll throw 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

看起来Pointer不会将指针指向正确的存储位置。

您的printf格式存在问题: %d用于整数。 请尝试使用%f

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM