在 Java 程序中调用 C# 方法 [英] Calling C# method within a Java program

查看:29
本文介绍了在 Java 程序中调用 C# 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于不同的原因,不能使用 JNI 在 Java 中直接调用 C# 方法.所以首先我们必须使用 C++ 为 C# 编写一个包装器,然后创建 dll 并通过 Java 中的 JNI 使用它.

C# methods cannot be called directly in Java using JNI due to different reasons. So first we have to write a wrapper for C# using C++ then create the dll and use it through JNI in Java.

我在 C++ 中调用 C# 代码时遇到问题.我正在将 C# .netmodule 文件添加到 C++ 项目中.代码粘贴在下面.如果我做错了什么,请指导我.

I have problem in calling C# code in C++. I'm adding C# .netmodule file to a C++ project. Code is pasted below. Please guide me if i'm doing anything wrong.

这是我的托管 C++ 类 UsbSerialNum.h:

This is my managed C++ class UsbSerialNum.h:

#using <mscorlib.dll>
#include <iostream>
#using "UsbSerialNumberCSharp.netmodule"

using namespace std;

using namespace System;

public __gc class UsbSerialNum
{
    public:

        UsbSerialNumberCSharp::UsbSerialNumberCSharp __gc *t;

        UsbSerialNum() {
            cout<<"Hello from C++";
            t = new UsbSerialNumberCSharp::UsbSerialNumberCSharp();
        }

        void CallUsbSerialNumberCSharpHello() {
            t->hello();
        }
};

C# UsbSerialNumberCSharp.cs 文件,我从中创建了 .netmodule 文件:

C# UsbSerialNumberCSharp.cs file from which i've created the .netmodule file:

using System.Collections.Generic;
using System.Text;

namespace UsbSerialNumberCSharp
{
    public class UsbSerialNumberCSharp
    {

        public UsbSerialNumberCSharp(){
            Console.WriteLine("hello");
        }

        public static void hello()
        {
            Console.WriteLine("hello");
        }

        public void helloCSharp ()
        {
            Console.WriteLine("helloCSharp");
        }
    }
}

这是我创建 makeDLL.dll 的主要 makeDLL.cpp 文件:

Here is my main makeDLL.cpp file from which makeDLL.dll is created:

#include "jni.h"
#include <iostream>


// This is the java header created using the javah -jni command.
#include "testDLL.h"


// This is the Managed C++ header that contains the call to the C#
#include "UsbSerialNum.h"

using namespace std;


JNIEXPORT void JNICALL Java_testDLL_hello
(JNIEnv *, jobject) {

    // Instantiate the MC++ class.
    UsbSerialNum* serial = new UsbSerialNum();
    serial->CallUsbSerialNumberCSharpHello();
}

这是我的 java 类:

Here is my java class:

public class testDLL {

    static {
        System.loadLibrary("makeDLL");
    }

    /**
     * @param args
     */
    public static void main (String[] args) {
        //        new testDLL().GetUSBDevices("SCR3", 100);
        new testDLL().hello();
    }

    public native void hello();

}

如果我只是在我的主文件中忽略对 UsbSerial.h 的调用,即使用简单的 C++,那么我的代码在 Java 中运行良好.基本上 C++ 托管类不能正常工作.请指导我.谢谢.

If i simply ignore the call to UsbSerial.h in my main file i.e. use simple C++ then my code is working fine in Java. Basically C++ managed class is not working properly. Please guide me. Thanks.

推荐答案

确切地知道你需要这种互操作性是有用的.无论如何,您应该查看 IKVM;或者,您可以(正如针对类似问题所建议的那样)使用 COM 作为桥梁:将 C#/CLR 公开为 COM 接口,然后使用 com4j 在 Java 中.

It would be useful to know what you need this interoperability for exactly. In any case, you should look into IKVM; alternatively you can (as has been suggested for a similar problem) use COM as a bridge: expose the C#/CLR as a COM interface and then use com4j in Java.

这篇关于在 Java 程序中调用 C# 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆