2013-03-25 46 views
0

我試圖從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); 

回答

2

this referenceD2D1CreateFactory需要指針類型作爲第三和第四個參數(您只聲明瞭三個參數)。

假設您插入選項指針(簡單的struct *),您的最終參數需要爲PointerByReference,因爲該函數將「返回」您提供的地址中的指針值。

然後可以使用PointerByReference.getValue()初始化新ID2D1Factory實例(在這種情況下,Structure.ByReference是多餘的,因爲默認情況下,所有結構作爲函數參數爲struct *由JNA除非另有明確定義處理)。

public WinNT.HRESULT D2D1CreateFactory(int factoryType, REFIID riid, D2D1_FACTORY_OPTIONS options, ID2D1Factory ppIFactory); 

public class D2D1_FACTORY_OPTIONS extends Structure { ... } 

D2D1_FACTORY_OPTIONS options = ...; 
PointerByReference pref = new PointerByReference(); 

D2D1.INSTANCE.D2D1CreateFactory(0, new REFIID(...), options, pref); 
ID2D1Factory factory = new ID2D1Factory(pref.getValue()); 

而且不要忘記調用Structure.read()ID2D1Factory(Pointer)構造函數。

+0

非常感謝。我會試試這個結果並回來告訴其他用戶。 – 2013-03-25 18:53:08