使用JNI集成java和.net dll

Integrating java and .net dll using JNI

本文关键字:net dll java JNI 集成 使用      更新时间:2023-10-16

我正在做一个小项目,它是java和.net dll的互操作性。

焦点:

我只有一个java文件,它调用了使用C#、CPP和MCPP创建的.net dll。。

这个节目只是一个你好世界节目。。

我只是参考下面提到的网站。

http://www.codeproject.com/Articles/378826/How-to-wrap-a-Csharp-library-for-use-in-Java

http://www.codeproject.com/Articles/13093/C-method-calls-within-a-Java-program

最后,我只是得到了一些想法,并最终做了一些错误。

编码:

#using "mscorlib.dll"
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
    public:
    // Provide .NET interop and garbage collecting to the pointer.
    CSharpHelloWorld __gc *t;
    HelloWorldC() {
        t = new CSharpHelloWorld();
        // Assign the reference a new instance of the object
    }
 // This inline function is called from the C++ Code
    void callCSharpHelloWorld() {
        t->displayHelloWorld();
    }
};

错误:

Error   1   error C4980: '__gc' : use of this keyword requires /clr:oldSyntax command line option   
Error   2   error C3699: 'interior_ptr' : cannot use this indirection on type 'CSharpHelloWorld'    
Error   3   error C2750: 'CSharpHelloWorld' : cannot use 'new' on the reference type; use 'gcnew' instead   
Error   4   error C2440: '=' : cannot convert from 'CSharpHelloWorld *' to 'CSharpHelloWorld ^' 11  WindowsComponentProject
Error   5   error C2011: 'HelloWorldC' : 'class' type redefinition  6   WindowsComponentProject
Error   6   error C3699: '*' : cannot use this indirection on type 'HelloWorldC'    18  WindowsComponentProject
Error   7   error C2750: 'HelloWorldC' : cannot use 'new' on the reference type; use 'gcnew' instead    18  WindowsComponentProject
Error   8   error C2440: 'initializing' : cannot convert from 'HelloWorldC *' to 'HelloWorldC ^'    18  WindowsComponentProject
Error   9   error C2027: use of undefined type 'HelloWorldC'        21  WindowsComponentProject
Error   10  error C2227: left of '->callCSharpHelloWorld' must point to class/struct/union/generic type  21 WindowsComponentProject

我只是在一些网站上寻找更改CLR属性的解决方案,但它不起作用。请帮助我解决这个问题。提前感谢!!!

您使用的是旧的托管C++语法。新的称为C++/CLI。

您可以通过在类声明前面加上ref关键字来创建引用类型。使用^可以像在CSharpHelloWorld^ t中一样声明引用变量。gcnew是用来在托管堆中创建对象的。

您应该修改您的类,如下所示。

using namespace System;
public ref class HelloWorldC { 
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld^ t;
HelloWorldC() {
    t = gcnew CSharpHelloWorld();
    // Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
    t->displayHelloWorld();
}
};