Re: [問題] JNative的問題
※ 引述《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)
討論串 (同標題文章)