简体   繁体   English

在没有第三方库的Java中使用C#DLL

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

I have a C# dll that I need to use in Java. 我有一个需要在Java中使用的C#dll。 The dll came with a .chm file that outlined the methods/fields of the classes. 该DLL带有一个.chm文件,该文件概述了类的方法/字段。 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. 在做了研究之后,我决定最好的方法是创建一个C ++ cli包装器并从那里使用JNI来获得我需要的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. Instance是一个静态字段,两个方法调用返回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/ 我试图将我的包装器放在这个例子的基础上: 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. 我没有使用JNI从Java调用C ++ cli DLL的问题,除了当我尝试在C ++ cli中从C#实例化类实例时,JVM崩溃了。

_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? 假设Instance成员是属性而不是字段,您可以尝试以下方式吗?

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

In C# properties are in fact methods. 在C#中,属性实际上是方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM