简体   繁体   English

使用函数指针为C结构创建JNA映射

[英]Create JNA mapping for C struct with function pointer

I am creating JNA mappings to the OpenMAX C library. 我正在创建到OpenMAX C库的JNA映射。 Along the way I am also learning C. I have encountered a struct that I am unsure how to map to, and that I have been unable to find any resources to help. 在学习C的过程中,我遇到了一个不确定该如何映射的结构,并且无法找到任何资源来提供帮助。

Here is a snippet from the struct 这是该结构的摘录

typedef struct OMX_COMPONENTTYPE {    

    OMX_VERSIONTYPE nVersion;

    OMX_ERRORTYPE (*SetParameter)(
                 OMX_HANDLETYPE hComponent, 
                 OMX_INDEXTYPE nIndex,
                 OMX_PTR pComponentParameterStructure);
...

"nVersion" is a normal member and is easily mappable in java. “ nVersion”是普通成员,在Java中很容易映射。

My problem is with the functional pointer SetParameter. 我的问题是函数指针SetParameter。 (I think that's what it is) (我认为就是这样)

In Java, structs get mapped to a child of the jna.Structure class. 在Java中,结构映射到jna.Structure类的子级。 Because this is a class (not an interface), I cannot define a method header without a body which is how I have otherwise mapped methods. 因为这是一个类(不是接口),所以如果没有主体,则无法定义方法标头,否则将无法映射主体。

Does anyone know what this mapping is supposed to look like? 有谁知道该映射的外观?

Thanks 谢谢

JNA uses Callback objects to represent function pointers, and includes a description of callback usage . JNA使用回调对象表示函数指针,并包括对回调用法的描述

Make an interface that derives from Callback , which implements a single method that matches your function pointer's. 创建一个从Callback派生的接口,该接口实现与您的函数指针匹配的单个方法。

public class MyStructure extends Structure {    

    public MyCallback callback;

    public interface MyCallback extends Callback {
        void invoke();
    }
}

If you read the struct from native memory, you'll get a proxy object you can use to call the function pointer. 如果您从本地内存中读取该结构,则将获得一个代理对象,可用于调用函数指针。

From Java code, you can then assign the field a new value like thus: 然后,可以从Java代码中为字段分配一个新值,如下所示:

MyStructure s = ...;
s.callback = new MyCallback() {
    public void invoke() {
        // your callback implementation here
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM