使用JNA将数组从Java传递到dll函数 [英] Passing array from java to dll function with JNA

查看:340
本文介绍了使用JNA将数组从Java传递到dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass Java array as parameter to c dll throw JNA , here is my code :

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

看起来像指针在正确的内存位置没有指向点.

Looks like the Pointer doest's points at the right memory place .

推荐答案

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

There's a problem with your printf format: %d is for integers. Try %f instead.

这篇关于使用JNA将数组从Java传递到dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆