使用JNI从Java调用C#方法

时间:2018-05-15 09:22:55

标签: java c# java-native-interface

我试图按照我在这里找到的示例,但我无法让它工作。 这是我的Java类

package jniTester;
public class JNITester {
    static {
        System.load("D:\\\\VisualStudio_Cpp_2017\\SkriptumTeil5\\Debug\\HelloWorldJNI.dll");
    }

    public static native String welcome(String name);
}

由此我用javah创建了jniTester.h文件

这是我的C#类

namespace HelloWorldJNI
{

    public static class HelloWorldJNI
    {
        public static String Welcome(String name)
        {
            return "Hello " + name + "! This is your C# buddy.";
        }
    }
}

由此我创建了HelloWorldJNI.netmodule

这是我的cpp类

#include "stdafx.h"
#include <jni.h>
#include <string>
#include "jniTester.h"
#using "D:\VisualStudio_C#_2017\SkriptumTeil5\HelloWorldJNI\HelloWorldJNI.netmodule"


using namespace std;

JNIEXPORT jstring JNICALL Java_jniTester_JNITester_welcome(JNIEnv *env, jclass thisclass, jstring inJNIStr) {
    // Step 1: Convert the JNI String (jstring) into C-String (char*)
    const char *inCStr = env->GetStringUTFChars(inJNIStr, NULL);
    if (NULL == inCStr) return NULL;

    // Step 2: Convert the C++ string to C-string, then to JNI String (jstring) and return
    //string outCppStr = "Hello " + std::string(inCStr) + ". Greetings from your C++ buddy";
    //env->ReleaseStringUTFChars(inJNIStr, inCStr);  // release resources
    //return env->NewStringUTF(outCppStr.c_str());

    //// Alternate Step 2:
    System::String^ outStr = HelloWorldJNI::HelloWorldJNI::Welcome(gcnew System::String(inCStr));
    env->ReleaseStringUTFChars(inJNIStr, inCStr);  // release resources
    char* converted = static_cast<char*>((System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(outStr)).ToPointer());
    return env->NewStringUTF(converted);
 }

步骤2下的代码有效。但是这不是我的C#方法。 备用步骤2下的实施失败,带

# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (0xe0434352), pid=37224, tid=0x00003350

我不是cpp专家,所以我完全处于黑暗中。这有什么不对?

1 个答案:

答案 0 :(得分:0)

首先,您需要使用COM接口在C#中创建汇编DLL。

然后,您可以创建一个本机(非托管)DLL包装器库,您可以将其与JNI一起使用。

您可以按照guide for Calling Managed NET COM Objects from Unmanaged C code

进行操作

您也可以查看JNI manual

一个简短的例子:

C#代码Managed.cs

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: ComVisible(true)] 
[assembly: AssemblyDelaySign(false)] 
[assembly:AssemblyKeyFileAttribute("Managed.snk")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ManagedClassLibrary {

    [Guid("1CE4ECCB-EB78-4A50-8781-444052C7AEAE")]
    [ComVisible(true)]
    public interface IArithmetic {
        int sum(int lsh, int rhs);
        int subtract(int lsh, int rhs);
    }
    [Guid("30F078F9-F161-4112-B61A-B3BD6B63CB4C"), 
        ClassInterface(ClassInterfaceType.None), 
        ComSourceInterfaces(typeof(IArithmetic))
    ]
    [ComVisible(true)]
    public class ArithmeticImpl:IArithmetic {

        public int sum(int lsh, int rhs)
        {
             return lsh + rhs;
        } 
        public int subtract(int lsh, int rhs)
        {
             return lsh - rhs;
        }
    }

}

C ++ dll包装器代码dotnet_arithmetic.cpp

#include <windows.h>
#include <Objbase.h>

#include <jni.h>

#import "Managed.tlb" named_guids raw_interfaces_only

using namespace Managed;

class DNArithmetic 
{
    DNArithmetic(const DNArithmetic&) = delete;
    DNArithmetic& operator=(const DNArithmetic&) = delete;
private:
    DNArithmetic() noexcept
    {
     mi_.CreateInstance(CLSID_ArithmeticImpl);
    }
public:
    const DNArithmetic* instanse() {
        if(!_co_init) { 
         ::CoInitialize(NULL);
         _co_init = true;
        }
        static DNArithmetic _ret;            
        return &_ret;
    }
    int sum(int lsh, int rhs) const {
        int ret;
        mi_->sum(lsh, rhs, &ret);
        return ret;
    }

    int subtract(int lsh, int rhs) const {
        int ret;
        mi_->subtract(lsh, rhs, &ret);
        return ret;
    }

    ~DNArithmetic() noexcept
    { 
      ::CoUninitialize();           
    }
private:
    IArithmeticPtr mi_;
    static bool _co_init;
};

bool DNArithmetic::_co_init = false;

extern "C" {

    JNIEXPORT jint JNICALL Java_Arithmetic_dotnet_1sum(JNIEnv *evn, jclass clazz, jint lsh, jint rhs)
    {
        return DNArithmetic::instance()->sum(lsh, rhs);
    }

    JNIEXPORT jint JNICALL Java_Arithmetic_dotnet_1subtract(JNIEnv *evn, jclass clazz, jint lsh, jint rhs)
    {
        return DNArithmetic::instance()->subtract(lsh, rhs);
    }

    BOOL WINAPI DllMain(::HMODULE hprocess,::DWORD fdwReason,::LPVOID lpvReserved)
    {
        switch (fdwReason) {
        case DLL_PROCESS_ATTACH:           
            break;
        case DLL_PROCESS_DETACH:                
            break;
        case DLL_THREAD_ATTACH:
           break;
        case DLL_THREAD_DETACH:
           break;
        }
        return TRUE; // successful
    }

}

Java Code Arithmetic.java

public class Arithmetic {

    private static native int dotnet_sum(int lsh, int rhs);

    private static native int dotnet_subtract(int lsh, int rhs);

    private Arithmetic() {
        try {
            System.loadLibrary("dotnet_arithmetic.dll");
        } catch (UnsatisfiedLinkError e) {
            throw new IllegalStateException(e);
        }
    }

    private int sum(int lsh, int rhs) {
        return  dotnet_sum(lsh, rhs);
    }

    private int subtract(int lsh, int rhs) {
        return dotnet_subtract(lsh,rhs);
    }

    public static void main(String[] args) {
        System.setProperty("java.library.path", System.getProperty("user.dir") );
        System.out.println("About to call C# functions from Java over the MS COM");
        Arithmetic instance = new Arithmetic();
        System.out.println("C# 1 + 2 = " + instance.sum(1,2) );
        System.out.println("C# 2 - 1 = " + instance.subtract(2,1) );
    }
}

构建文件build.cmd(用于演示的简单bat文件)

@echo off
rem make sure you have JAVA_HOME system variable set
rem Please set your VS root dir, should be  somewhere in C:\Program Files 
SET VS_HOME= 
echo %VS_HOME%

echo Take Visual studio command line tools
    call %VS_HOME%\VC\Auxiliary\Build\vcvars64.bat

echo Build C# class library
    sn /k Managed.snk
    csc /optimize /target:library /out:Managed.DLL Managed.cs
    RegAsm  Managed.DLL /tlb:Managed.tlb
    gacutil /i Managed.DLL 

echo Build C++ wrapper JNI DLL
    cl /c /nologo /GL /Zl /std:c++latest /I%JAVA_HOME%\include /I%JAVA_HOME%\include\win32 /Fodotnet_arithmetic.obj dotnet_arithmetic.cpp
    link /DLL /LTCG /LIBPATH:%JAVA_HOME%\lib /OUT:dotnet_arithmetic.dll msvcrt.lib kernel32.lib Ole32.lib jvm.lib dotnet_arithmetic.obj

echo Build Java

javac Arithmetic.java

echo Running the Demo

java Arithmetic

pause

注意:此解决方案仅适用于Windows操作系统。并且不应该适用于Unix上的Mono。