从JNA调用读取String []失败

时间:2018-07-10 11:29:03

标签: java jna

我正在尝试通过JNA调用以下函数:

  

结构组* getgrnam(const char * name)

,如以下内容所述: http://www.gnu.org/software/libc/manual/html_node/Lookup-Group.html#Lookup-Group

根据文档,结构如下:

  

字符* gr_name

     

组名。

     

gid_t gr_gid

     

组的组ID。

     

char ** gr_mem

     

指向组中用户名的指针的向量。每个用户名都是一个以空字符结尾的字符串,向量本身以空指针结尾。

我创建了以下简单的测试类

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class Test {

    public interface CLibrary extends Library {
            CLibrary INSTANCE = (CLibrary)  Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);

            void printf(String format, Object... args);

            public Group getgrnam(String groupName);
        }


        public static void main(String[] args) {
            CLibrary.INSTANCE.printf("Hello, World\n");

            Group group = CLibrary.INSTANCE.getgrnam(args[0]);

            System.out.println(group.gr_name);
            System.out.println(group.gr_mem);

        }
    }

和代表结构的Group类:

import java.util.ArrayList;
import java.util.List;

import com.sun.jna.Structure;

public class Group extends Structure {
    public String gr_name;
    public int gr_gid;
    public String[] gr_mem = new String[128];

    @Override
    protected List<String> getFieldOrder() {
        List<String> fields = new ArrayList<>();
        fields.add("gr_name");
        fields.add("gr_gid");
        fields.add("gr_mem");
        return fields;
    }
}

marshalling documentation of JNA指出char **被转换为String []。

但是,当我运行此代码时,出现以下错误:

  

/ tmp#java -cp。:jna-4.5.1.jar测试根Hello,World Exception in   线程“主要” java.lang.IllegalArgumentException:读取数组   不支持内存中的类java.lang.String       在com.sun.jna.Pointer.readArray(Pointer.java:538)       在com.sun.jna.Pointer.getValue(Pointer.java:459)       在com.sun.jna.Structure.readField(Structure.java:720)       在com.sun.jna.Structure.read(Structure.java:580)       在com.sun.jna.Structure.autoRead(Structure.java:2074)       在com.sun.jna.Structure.conditionalAutoRead(Structure.java:550)       在com.sun.jna.Function.invoke(Function.java:446)       在com.sun.jna.Function.invoke(Function.java:354)       在com.sun.jna.Library $ Handler.invoke(Library.java:244)       在com.sun.proxy。$ Proxy0.getgrnam(未知来源)       在Test.main(Test.java:23)

如何正确转换char **的结构字段?

1 个答案:

答案 0 :(得分:1)

自我回答,因为我是在挖掘后才弄清楚的。

此工具的主要突破是:https://github.com/nativelibs4java/JNAerator

它从C结构生成Java类,这很有帮助!

最后,正确映射组结构的Java类是这样的:

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.PointerByReference;
import java.util.Arrays;
import java.util.List;
/**
 * <i>native declaration : line 2</i><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
 */
public class Group extends Structure {
  /**
   * Group name.<br>
   * C type : char*
   */
  public Pointer gr_name;
  /**
   * Password.<br>
   * C type : char*
   */
  public Pointer gr_passwd;
  /**
   * Group ID.<br>
   * C type : __gid_t
   */
  public int gr_gid;
  /**
   * Member list.<br>
   * C type : char**
   */
  public PointerByReference gr_mem;
  public Group() {
    super();
  }
  protected List<String> getFieldOrder() {
    return Arrays.asList("gr_name", "gr_passwd", "gr_gid", "gr_mem");
  }
  /**
   * @param gr_name Group name.<br>
   * C type : char*<br>
   * @param gr_passwd Password.<br>
   * C type : char*<br>
   * @param gr_gid Group ID.<br>
   * C type : __gid_t<br>
   * @param gr_mem Member list.<br>
   * C type : char**
   */
  public Group(Pointer gr_name, Pointer gr_passwd, int gr_gid, PointerByReference gr_mem) {
    super();
    this.gr_name = gr_name;
    this.gr_passwd = gr_passwd;
    this.gr_gid = gr_gid;
    this.gr_mem = gr_mem;
  }
  public Group(Pointer peer) {
    super(peer);
  }
  protected ByReference newByReference() { return new ByReference(); }
  protected ByValue newByValue() { return new ByValue(); }
  protected Group newInstance() { return new Group(); }
//  public static Group[] newArray(int arrayLength) {
//    return Structure.newArray(Group.class, arrayLength);
//  }
  public static class ByReference extends Group implements Structure.ByReference {

  };
  public static class ByValue extends Group implements Structure.ByValue {

  };
}

在gr_mem字段(C中为char **类型)中使用PointerByReference是一个突破。

之后可以这样读取:

  public static void main(String[] args) {

    Group group = CLibrary.INSTANCE.getgrnam(args[0]);
    System.out.println(group.gr_name.getString(0));

    PointerByReference pbr = group.gr_mem;
    String[] groups = pbr.getPointer().getStringArray(0);

    for (String g : groups) {
      System.out.println(g);
    }