zDroid
zDroid

Reputation: 426

Accessing DLL with multiple classes in java

I have to access a third party dll which has multiple classes in my Java application. I checked this http://blog.mwrobel.eu/how-to-call-dll-methods-from-java/

But its describing a dll with just a class and some methods. How do I access each specific class and its methods in the dll. Is this possible?

Upvotes: 1

Views: 866

Answers (2)

technomage
technomage

Reputation: 10069

You need to wrap the functionality you need in C-compatible functions (extern "C").

For example, if I have a C++ class as follows:

class Foo {
    Foo();
    ~Foo();
    void bar();
}

Then I would need to create some wrapper functions in the shared library that will be accessible via JNA:

extern "C" void* create_foo() { return new Foo(); }
extern "C" void delete_foo(void *foo) { delete (Foo *)foo; }
extern "C" void bar(void *foo) { ((Foo *)foo)->bar(); }

If you have to do a lot of this and compile some native code anyway, you might find using SWIG to be of use. You can customize how your native classes map into Java classes and vice versa.

Upvotes: 1

Lemonov
Lemonov

Reputation: 496

Did you try adapting the interface from the examples from your link. I think that if you make similar interface with loadLibrary("otherClassName",...) you will be able to load another class from this interface.

Upvotes: 0

Related Questions