我試圖從DLL D2D1.dll映射函數D2D1CreateFactory。從那裏我想創建一個Direct2D Java映射,但這是無關緊要的。我至今有這樣的:來自D2D1的Java JNA映射D2D1CreateFactory
public WinNT.HRESULT D2D1CreateFactory(int factoryType, REFIID riid, ID2D1Factory.ByReference ppIFactory);
的ID2D1Factory看起來是這樣的:
public class ID2D1Factory extends IUnknown {
public ID2D1Factory() { }
public ID2D1Factory(Pointer pvInstance) {
super(pvInstance);
}
}
當我嘗試使用下面的代碼來運行我的代碼,「java.lang.Error的:無效的內存訪問」是拋出(打開JNA.setProtected()時)。
的代碼運行:
ID2D1Factory.ByReference ref= new ID2D1Factory.ByReference();
D2D1.INSTANCE.D2D1CreateFactory(0, new REFIID(new IID("06152247-6f50-465a-9245-118bfd3b6007").toByteArray()), ref);
我不知道爲什麼。有什麼我做錯了嗎?
編輯:感謝technomage我能夠得到正確的方法聲明。該方法應該聲明如下:
public WinNT.HRESULT D2D1CreateFactory(int factoryType, REFIID riid, D2D1_FACTORY_OPTIONS opts, PointerByReference pref);
的D2D1_FACTORY_OPTIONS結構映射如下:
public static class D2D1_FACTORY_OPTIONS extends Structure {
public int debugLevel;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "debugLevel" });
}
public D2D1_FACTORY_OPTIONS() {}
public D2D1_FACTORY_OPTIONS(int size) {
super(new Memory(size));
}
public D2D1_FACTORY_OPTIONS(Pointer memory) {
super(memory);
read();
}
}
最後,該代碼段調用的方法:
D2D1_FACTORY_OPTIONS opts = new D2D1_FACTORY_OPTIONS();
PointerByReference pp = new PointerByReference();
D2D1.INSTANCE.D2D1CreateFactory(0, new REFIID(new IID("06152247-6f50-465a-9245-118bfd3b6007").toByteArray()), opts, pp);
非常感謝。我會試試這個結果並回來告訴其他用戶。 – 2013-03-25 18:53:08