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

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

问题描述

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

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.cpp 文件从 makeDLL.dll 创建:

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天全站免登陆