无符号类型的BridJ类型映射

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

使用JNAerator生成BridJ代码时,它会将无符号类型(例如windows'ULONG)映射到普通的Java long

// c code
typedef struct _S {
  USHORT    a;
  ULONG     b;
  ULONG64   c;
} S;


// generated java code
class S extends StructObject {

@Field(0)
public short a() {
    return this.io.getShortField(this, 0);
}

@Field(0)
S setA(short a) {
    this.io.setShortField(this, 0, a);
    return this;
}

@CLong
@Field(1)
public long b() {
    return this.io.getCLongField(this, 2);
}

@CLong
@Field(1)
S setB(long b) {
    this.io.setCLongField(this, 2, b);
    return this;
}

// ULONG64 is ignored and not generated at all

但是,Java类型是签名的,而不是未签名的。如果我需要手动更正,我需要使用哪些类型?像这样的字节数组?

@Field(0)
public byte[] a() { ... };

@Field(0)
public byte[] setA(byte[] a) { // check correct length };
java java-native-interface jna jnaerator bridj
1个回答
1
投票

有符号和无符号类型具有相同的大小,只是不同的语义。您绝对应该使用JNAerator选择的类型,并使用处理无符号类型的Java原语,例如the ones introduced in Java 8(例如,Integer.divideUnsigned)。

如果Java 8不适合您,您可以只使用use casts and manipulate bits wisely

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