将JNA Java映射到Native C共享库上

问题描述 投票:-2回答:1

试图将以下内容从Java代码传递到一个共享的C库.so。I am very new to JNA if someone would point me i the right direction I would be very grateful.

我得到的错误是:java.lang.Error.Structure.getFieldOrder()on classing.Implement.FieldOrder()。Structure.getFieldOrder() on class com.dataTypes.TCIMUEvent does not provide enough names [0] ([]) to match declared fields [11] ([accel, accelValid, gyro, gyroValid, mag, magValid, pressure, pressureValid, temperature, temperatureValid, time])

两个相关的ObjectsStructure。

public class Motion extends Structure {

    public TCIMUEvent tcimuEvent;
    public int Status;
    public double userHeadingDeg;
    public float rotationMode;

    public Motion() {
    }

    public TCIMUEvent getTcimuEvent() {
        return tcimuEvent;
    }

    public Motion(TCIMUEvent tcimuEvent, int status, double userHeadingDeg, float rotationMode) {
        this.tcimuEvent = tcimuEvent;
        Status = status;
        this.userHeadingDeg = userHeadingDeg;
        this.rotationMode = rotationMode;
    }
public class TCIMUEvent extends Structure {

    public double time;
    public float[] accel;
    public boolean accelValid;
    public float[] mag;
    public boolean magValid;
    public float[] gyro;
    public boolean gyroValid;
    public float pressure;
    public boolean pressureValid;
    public float temperature;
    public boolean temperatureValid;

    public TCIMUEvent(double time, float[] accel, boolean accelValid, float[] mag, boolean magValid, float[] gyro, boolean gyroValid, float pressure, boolean pressureValid, float temperature, boolean temperatureValid) {
        this.time = time;
        this.accel = accel;
        this.accelValid = accelValid;
        this.mag = mag;
        this.magValid = magValid;
        this.gyro = gyro;
        this.gyroValid = gyroValid;
        this.pressure = pressure;
        this.pressureValid = pressureValid;
        this.temperature = temperature;
        this.temperatureValid = temperatureValid;
    }

C样本stuct


typedef struct {
  double time;
  double accel[3];
  bool accelValid;
  double mag[3];
  bool magValid;
  double gyro[3];
  bool gyroValid;
  double pressure;
  bool pressureValid;
  double temperature;
  bool temperatureValid;
} T_imuDataSample_t;

处理数据的C文件

void HandleImuEvent(T_imuDataSample_t *imuDataSample, int *status,
  double *userHeadingDeg, StrapdownStreaming_RotationMode *currentRotateMode)
{

这是数据从Java传递到C语言的地方。

HandleImuEvent(motion.getTcimuEvent(), motion.getStatus(), motions.getUserHeadingDeg(), motions.getRotationMode());

错误的完整跟踪是:

    java.lang.Error: Structure.getFieldOrder() on class com.dataTypes.TCIMUEvent does not provide enough names [0] ([]) to match declared fields [11] ([accel, accelValid, gyro, gyroValid, mag, magValid, pressure, pressureValid, temperature, temperatureValid, time])
        at com.sun.jna.Structure.getFields(Structure.java:1077)
        at com.sun.jna.Structure.deriveLayout(Structure.java:1232)
        at com.sun.jna.Structure.calculateSize(Structure.java:1159)
        at com.sun.jna.Structure.allocateMemory(Structure.java:401)
        at com.sun.jna.Structure.ensureAllocated(Structure.java:377)
        at com.sun.jna.Structure.ensureAllocated(Structure.java:367)
        at com.sun.jna.Structure.size(Structure.java:434)
        at com.sun.jna.Structure.size(Structure.java:1137)
        at com.sun.jna.Native.getNativeSize(Native.java:1354)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2253)
        at com.sun.jna.Structure.getNativeSize(Structure.java:2243)
        at com.sun.jna.Structure.validateField(Structure.java:1209)
        at com.sun.jna.Structure.validateFields(Structure.java:1222)
        at com.sun.jna.Structure.<init>(Structure.java:200)
        at com.sun.jna.Structure.<init>(Structure.java:193)
        at com.sun.jna.Structure.<init>(Structure.java:180)
        at com.sun.jna.Structure.<init>(Structure.java:172)
        at com.dataTypes.Motion.<init>(Motion.java:17)

我感觉我对JNA的功能有点天真,所以问题在于我对如何传递数据的理解,所以关于这个具体实现的一般性建议肯定会帮助我理解使用JNA需要哪些步骤。我已经看了很多教程,目前正在查看这里的其他答案。如果你需要更多的信息或背景,请给我一个呼喊。

java c java-native-interface jna java-native-library
1个回答
0
投票

正如错误所说,你缺少一个 GetFieldOrder 方法。

文件:

你必须定义getFieldOrder()来返回一个字段名(Strings)的列表,表示字段的正确顺序。当处理Structure的多级子类时,你必须将当前类中定义的字段添加到超类getFieldOrder()提供的列表中。

您也可以使用 @Structure.FieldOrder 注释 来提供字段顺序,而不是定义一个方法。

© www.soinside.com 2019 - 2024. All rights reserved.