我试图从 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);