作者sbrhsieh (偶尔想摆烂一下)
看板java
标题Re: [问题] JNative的问题
时间Mon May 31 15:44:56 2010
※ 引述《Nt1 (用功点吧!)》之铭言:
: 请问有人研究过 JNative 吗?
: JNative是一个可以透过 java 去 call dll的东西,假设我一个dll的function是这样:
: void Test(int a, int b, int c){
: a = xxx;
: b = 000;
: c = zzz;
: }
: 以 c 来说,这个function会将某些特定的直写到 a, b, c 三个变数中,也就是传进来
: a b c不是给值,而只是给个容器而已。
out parameter 应是 pointer type。
: 以 JNative来说..要怎麽做到这样呢?
假设 dll export 如下的 function:
void Test(/* out */ int *n,/* out */ float *f,/* out */ double *d);
client code:
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
...
public static void main(String[] args) throws NativeException, IllegalAccessException {
final String dllPath = ...;
JNative nfunc = new JNative(dllPath, "Test");
Pointer nP = new Pointer(MemoryBlockFactory.createMemoryBlock(4)); // int
Pointer fP = new Pointer(MemoryBlockFactory.createMemoryBlock(4)); // float
Pointer dP = new Pointer(MemoryBlockFactory.createMemoryBlock(8)); // double
nfunc.setRetVal(Type.VOID);
nfunc.setParameter(0, nP);
nfunc.setParameter(1, fP);
nfunc.setParameter(2, dP);
nfunc.invoke();
System.out.println("n=" + nP.getAsInt(0));
System.out.println("f=" + fP.getAsFloat(0));
System.out.println("d=" + dP.getAsDouble(0));
nP.dispose();
fP.dispose();
dP.dispose();
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.173.130.72
※ 编辑: sbrhsieh 来自: 218.173.130.72 (05/31 16:00)