我有以下 C 代码,我试图将其映射到 java 中:
int32 Query( void* qryBuffer,
uint32_t qryLength)
{
static int8_t sendBuffer[MAX_REQUEST_SIZE];
message* queryMessage;
...
queryMessage = (message*)sendBuffer;
memcpy( &sendBuffer[0], &qryLength, sizeof(qryLength) );
memcpy( &sendBuffer[sizeof(qryLength)], qryBuffer, qryLength );
...
query_ex(queryMessage);
}
typedef struct
{
uint32 length_u;
} message;
消息结构的 JNA 等效项是:
public static class message extends Structure {
public int length_u;
public message() {
super();
}
protected List getFieldOrder() {
return Arrays.asList("length_u");
}
public message(int length_u) {
super();
this.length_u = length_u;
}
public static class ByReference extends omni_message implements Structure.ByReference {
};
public static class ByValue extends omni_message implements Structure.ByValue {
};
};
我知道他们在 C 代码中做了什么,但我不知道如何在 Java 端做到这一点。我需要让 Java 消息类包含传入的参数的长度和数组
请帮忙!
更新:我创建了自己的包含字节 [] 的消息结构副本:
public static class messageEx extends Structure {
public byte[] buffer;
...
}
我将尝试将这个结构传递给另一个结构,但我要到明天才能测试。
int size = struct.size();
byte[] buffer = new byte[size];
ByteBuffer bb = ByteBuffer.wrap(buffer);
bb.putInt(size);
bb.put(struct.getPointer().getByteArray(0, size));
messageEx msg = new messageEx(buffer);
query_ex(msg); // call to native world