gpt4 book ai didi

Java JNA 从 D2D1 映射 D2D1CreateFactory

转载 作者:行者123 更新时间:2023-11-28 03:20:19 25 4
gpt4 key购买 nike

我试图从 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:无效的内存访问”(当打开 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);

最佳答案

根据 this reference , 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());

并且不要忘记在您的 ID2D1Factory(Pointer) 构造函数中调用 Structure.read()

关于Java JNA 从 D2D1 映射 D2D1CreateFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com