Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java instantiate C++ class from DLL

Tags:

java

c++

dll

I wrote a set of C++ classes and created a DLL that exports one of these C++ classes. I need to instantiate the exported C++ class in a Java class. Is that possible?

I searched the web for a possible solution, but all I have found where solutions using JNA or JNI that import C++ functions only.

like image 270
LaDude Avatar asked Aug 15 '26 19:08

LaDude


1 Answers

Yes, you can instantiate a C++ class from Java.

One way is with SWIG, which can generate Java wrappers for C++ classes.

For example, given a C++ class like this:

class MyClass { 
public:
     MyClass();
     int myMethod( int arg );
}

SWIG allows you to write Java code like this:

MyClass myclass = new MyClass();
int val = myClass.myMethod( 42 );
like image 196
Andy Thomas Avatar answered Aug 17 '26 10:08

Andy Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!