我用jnative调用C++写的动态链接库,调用函数获得函数返回值,定义指针获取返回参数但是总是报错,去掉指针定义就能执行成功但是不定义指针怎样获取返回参数啊~~有做过的朋友么,指点一二共同进步,下面是我的代码
public class Jna {
public static void main(String[] arge) throws NativeException, IllegalAccessException, UnsupportedEncodingException{
Jna jna=new Jna();
jna.getDate();
}
public void getDate() throws NativeException, IllegalAccessException, UnsupportedEncodingException{
JNative jnative=new JNative("BJICKDLL.dll","ReadUserInfo_ForJava2");
Pointer pointer1=new Pointer(MemoryBlockFactory.createMemoryBlock(4*200));
jnative.setParameter(0, pointer1);
此处定义指针获得返回参数但是函数执行不成功,去掉定义函数执行成功 但是这样怎样获取函数返回参数 pointer1.setIntAt(0, 4*200);
jnative.setRetVal(Type.INT);
jnative.invoke();
System.out.println("返回"+jnative.getRetVal());
System.out.println("返回"+pointer1.getAsString());
}
public class Jna {
public static void main(String[] arge) throws NativeException, IllegalAccessException, UnsupportedEncodingException{
Jna jna=new Jna();
jna.getDate();
}
public void getDate() throws NativeException, IllegalAccessException, UnsupportedEncodingException{
JNative jnative=new JNative("BJICKDLL.dll","ReadUserInfo_ForJava2");
Pointer pointer1=new Pointer(MemoryBlockFactory.createMemoryBlock(4*200));
jnative.setParameter(0, pointer1);
此处定义指针获得返回参数但是函数执行不成功,去掉定义函数执行成功 但是这样怎样获取函数返回参数 pointer1.setIntAt(0, 4*200);
jnative.setRetVal(Type.INT);
jnative.invoke();
System.out.println("返回"+jnative.getRetVal());
System.out.println("返回"+pointer1.getAsString());
}
public interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),CLibrary.class); CLibrary INSTANCE01 = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "MediaInfo" : "c"),CLibrary.class); void printf(String format,Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n"); for (int i=0;i < args.length;i++) { CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]); }
}
}
这是我调用成功的。