[英]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.