简体   繁体   English

Java JNA C ++对方法映射

[英]Java JNA C++ pair method mapping

I have a simple dll that exposes a method with return type 我有一个简单的dll,公开了带有返回类型的方法

std::pair<int, string>

I am using JNA and I was wondering how can a pair structure be mapped using the Structure base class. 我正在使用JNA,我想知道如何使用Structure基类映射对结构。 Can something like Pair<T,E> extends Structure be done? 诸如Pair<T,E> extends Structure吗?

Thanks. 谢谢。

The short answer is no, you can't map C++ templates into Java Generics. 简短的答案是不,您不能将C ++模板映射到Java泛型中。 While they look similar, they're entirely different things. 尽管它们看起来很相似,但它们是完全不同的东西。

The slightly longer answer, is yes, you can map it, although the process is manual. 稍长一点的答案是,可以,但可以将其映射,尽管该过程是手动的。 If this is intended to run on a single platform, it might be worth the trouble. 如果打算在单个平台上运行,则可能会遇到麻烦。

First determine the data offsets of your pair, then make a JNA Structure with fields at offsets corresponding to your pair data offsets. 首先确定对的数据偏移量,然后制作一个JNA结构,其字段的偏移量对应于对的数据偏移量。

// C++
typedef std::pair<int,string> mypair;
mypair* p = (mypair *)0;
offset_t PADDING1 = (char*)&p->first - (char*)p;
offset_t PADDING2 = (char*)&p->second - (char *)p;

// Java
class MyPair extends Structure {
    public byte[] = byte[PADDING1]; // omit if PADDING1 is zero
    public first;
    public byte[] = byte[PADDING2]; // omit if PADDING2 is zero
    public second;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM