【问题标题】:Integrating java and .net dll using JNI使用 JNI 集成 java 和 .net dll
【发布时间】:2015-04-09 03:47:42
【问题描述】:

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

重点:

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

该程序只是一个hello world程序..

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

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 属性的网站,但它不起作用请帮助我解决它..提前谢谢!!!

【问题讨论】:

    标签: java c# c++ .net dll


    【解决方案1】:

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

    您可以通过在类声明前加上 ref 关键字来创建引用类型。使用^ 声明引用变量,如CSharpHelloWorld^ tgcnew 用于在托管堆中创建对象。

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

    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();
    }
    };
    

    【讨论】:

    • 错误 1 ​​错误 C2011: 'HelloWorldC' : 'class' 类型重定义 \HelloWorld.h 6 WindowsComponentProject
    • 错误 2 错误 C3699: '*' : 不能在类型 'HelloWorldC' \HelloWorld.cpp 18 WindowsComponentProject 上使用此间接寻址
    • 错误 3 错误 C2750: 'HelloWorldC' : cannot use 'new' on the reference type;使用 'gcnew' 代替 \HelloWorld.cpp 18 WindowsComponentProject
    • 问题是我无法正确加载头文件,请问如何加载头文件..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多