i have 1 c++ dll file. , know methods used in it. need call these methods java code. don't have access modify dll file. please provide me solution this.
i created javacpp purpose. i'll copy/paste sample code , explanations page:
the common use case involves accessing legacy library written c++, example, inside file named legacylibrary.h containing c++ class:
#include <string> namespace legacylibrary { class legacyclass { public: const std::string& get_property() { return property; } void set_property(const std::string& property) { this->property = property; } std::string property; }; }
to job done javacpp, can define java class such one--although 1 use parser produce header file demonstrated below:
import com.googlecode.javacpp.*; import com.googlecode.javacpp.annotation.*; @platform(include="legacylibrary.h") @namespace("legacylibrary") public class legacylibrary { public static class legacyclass extends pointer { static { loader.load(); } public legacyclass() { allocate(); } private native void allocate(); // call getter , setter functions public native @stdstring string get_property(); public native void set_property(string property); // access member variable directly public native @stdstring string property(); public native void property(string property); } public static void main(string[] args) { // pointer objects allocated in java deallocated once become unreachable, // c++ destructors can still called in timely fashion pointer.deallocate() legacyclass l = new legacyclass(); l.set_property("hello world!"); system.out.println(l.property()); } }
alternately, can produce java interface parsing header file config class such one:
@properties(target="legacylibrary", value=@platform(include="legacylibrary.h")) public class legacylibraryconfig implements parser.infomapper { public void map(parser.infomap infomap) { } }
and following build commands:
$ javac -cp javacpp.jar legacylibraryconfig.java $ java -jar javacpp.jar legacylibraryconfig $ javac -cp javacpp.jar legacylibrary.java $ java -jar javacpp.jar legacylibrary
for more complex examples including maven/ide integration, check out javacpp presets!
Comments
Post a Comment