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

how to specify a JNR Pointer like that of python ctypes

使用 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 字段键入为指向字符串的指针?

我不熟悉 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());
}