简体   繁体   中英

Java JNA C++ pair method mapping

I have a simple dll that exposes a method with return type

std::pair<int, string>

I am using JNA and I was wondering how can a pair structure be mapped using the Structure base class. Can something like Pair<T,E> extends Structure be done?

Thanks.

The short answer is no, you can't map C++ templates into Java Generics. 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.

// 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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