简体   繁体   中英

Using a C# DLL in Java without 3rd party libraries

I have a C# dll that I need to use in Java. The dll came with a .chm file that outlined the methods/fields of the classes. After doing research I decided the best way to do it would be to create a C++cli wrapper and use JNI from there to get what I needed to Java.

The way the class is designed to be used is like

MyCSharpClass myC = MyCSharpClass.Instance; //Instance is a static field of MyCSharpClass
myC.setState("ON"); // Accepts a string, returns an int
myC.getNetwork(); // Returns a string

Instance is a static field, and the two method calls there return int.

I was trying to base my wrapper off of the example here: http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/

I have no issue with using JNI to call into the C++cli DLL from Java, except for when I try to instantiate the class instance from C# in C++cli the JVM crashes.

_private->myCSharpClass= gcnew MyDLL::MyCSharpClass(); 

Does not work because the class has no constructor and

_private->myCSharpClass= MyDLL::MyCSharpClass::Instance;

does not work either.

I am pretty lost on what do to. I have read post and examples for hours without any real success. Any help would be much appreciated.

Assuming that the Instance member is a property and not a field, could you try the following way?

_private->myCSharpClass= MyDLL::MyCSharpClass::get_Instance()

In C# properties are in fact methods.

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