0

我试图从 DLL D2D1.dll 映射函数 D2D1CreateFactory。从那里我想建立一个 Direct2D Java 映射,但那是题外话。到目前为止,我有这个:

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: Invalid memory access”(打开 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);
4

1 回答 1

2

根据这个参考D2D1CreateFactory需要指针类型作为第三个和第四个参数(你只声明了三个参数)。

假设您插入选项指针(一个简单的struct *),您的最后一个参数需要是PointerByReference,因为该函数将在您给它的地址中“返回”一个指针值。

然后,您可以使用PointerByReference.getValue()来初始化一个新ID2D1Factory实例(Structure.ByReference在这种情况下是多余的,因为默认情况下,所有作为函数参数的结构都被 JNA 视为struct *除非另有明确定义)。

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)医生。

于 2013-03-25T18:20:20.413 回答