我正在尝试通过拖放将电子邮件从Outlook传输到Eclipse RCP应用程序。使用此Code Snippet我发现在从Outlook 2010到Java的拖放操作期间传输了以下本机类型:
我需要完整的邮件正文,因此在拖放操作期间提供的文本是不够的。我试图扩展ByteArrayTransfer以将本机对象转换为Java对象,从而提供对电子邮件的访问。像FileGroupDescriptor这样的结构是本机C结构。我尝试使用JNA读出它们,但是JNA无法将C结构转换为我的Structure类的对象。
我有两个问题:
来自扩展的ByteArrayTransfer类的代码:
public class FileGroupDescriptor extends Structure {
public int cItems;
public FileDescriptor[] fgd;
public FileGroupDescriptor() {
super();
}
public FileGroupDescriptor(Pointer pointer) {
super(pointer);
}
}
public Object nativeToJava(TransferData transferData) {
if (transferData.type == 49478) {
Native.setProtected(true);
byte[] buffer = (byte[]) super.nativeToJava(transferData);
Memory memory = new Memory(buffer.length);
memory.write(0, buffer, 0, buffer.length - 1);
Pointer p = memory.getPointer(0);
FileGroupDescriptor groupDescriptor = new FileGroupDescriptor(p);
System.out.println(groupDescriptor.cItems);
}
return "";
}
答案 0 :(得分:1)
名义上,这是您需要初始化JNA结构的方式。
public class FileGroupDescriptor extends Structure {
public int cItems;
public FileDescriptor[] fgd;
public FileGroupDescriptor(Pointer pointer) {
super(pointer);
this.cItems = pointer.readInt(0);
this.fgd = new FileDescriptor[this.cItems];
this.read();
}
}
这应足以在fgd
字段中提供您要查找的信息。您还应该将整个byte[]
长度写入内存;不确定为什么要省略最后一个字节(这不是C字符串)。