Radu
Radu

Reputation: 1104

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.

Upvotes: 0

Views: 443

Answers (1)

technomage
technomage

Reputation: 10069

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

Upvotes: 1

Related Questions