如何指定像python ctypes这样的JNR指针

时间:2018-12-04 08:02:23

标签: java jnr

使用python的ctypes,可以指定一个采用类型的指针:

class METADATA(Structure):
    _fields_ = [("classes", c_int),
                ("names", POINTER(c_char_p))]

使用JNR,它看起来像这样:

public static class Metadata extends Struct{

  public Metadata(jnr.ffi.Runtime rt) {
    super(rt);
  }

  public final Struct.Unsigned32 classes = new Struct.Unsigned32();
  public final Struct.Pointer names = new Struct.Pointer();
}

但是,可以输入names字段作为指向字符串的指针吗?

1 个答案:

答案 0 :(得分:4)

我对python的ctypes不熟悉,但是假设names的类型是char*char**,则可以尝试使用以下方法之一。

对于共享库,

#include <stdlib.h>
#include <stdio.h>

struct MyStruct {
    int classes;
    char *names;
    char **names2;
};

struct MyStruct *get_my_struct() {
    struct MyStruct *my_struct = malloc(sizeof(struct MyStruct));
    my_struct->classes = 42;
    my_struct->names = "My Names";
    char **names2 = calloc(2, sizeof(char *));
    names2[0] = "Stack";
    names2[1] = "Overflow";
    my_struct->names2 = names2;
    return my_struct;
}

结构可以定义如下

public static class MyStruct extends Struct {
  public MyStruct(Runtime runtime) {
    super(runtime);
  }

  public final Struct.Signed32 classes = new Struct.Signed32();
  // For char* the support is built-in
  public final Struct.String names = new Struct.UTF8StringRef();
  // For char** you could wrap a pointer and override getStringMemory
  public final UTF8StringRef[] names2 = UTF8StringRefArray(new Struct.Pointer(), 2);

  protected UTF8StringRef[] UTF8StringRefArray(Pointer pointer, int stringLength) {
    UTF8StringRef[] array = new UTF8StringRef[stringLength];
    for (int i = 0; i < array.length; i++) {
      int index = i;
      array[i] = new UTF8StringRef() {
        @Override
        protected jnr.ffi.Pointer getStringMemory() {
          return pointer.get().getPointer(getRuntime().addressSize() * index);
        }
      };
    }
    return array;
  }
}

对于上述内容,以下代码将打印42 My Names are Stack, Overflow

public interface MyLib {
  MyStruct get_my_struct();
}

public static void main(String[] args) {
  MyLib mylib = LibraryLoader.create(MyLib.class).load("mylib.so");
  MyStruct myStruct = mylib.get_my_struct();
  System.out.printf("%d %s are %s, %s", myStruct.classes.get(), myStruct.names.get(),
      myStruct.names2[0].get(), myStruct.names2[1].get());
}