使用JNI调用第三方.NET DLL [英] Calling a 3rd party .NET DLL using JNI

查看:146
本文介绍了使用JNI调用第三方.NET DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从以下位置调用第三方.NET DLL(摘自此处)在JAVA程序中. 在此处

I'm trying to call a 3rd party .NET DLL (Taken from here) from within a JAVA program. After looking here and here I managed to get the whole thing to compile and run. But I get an exception when running the .NET code:

Java运行时环境已检测到严重错误

fatal error has been detected by the Java Runtime Environment

仅当我尝试从.NET DLL中访问另一个.net对象和方法时,才会发生这种情况:

This only happens when I try to access another .net object and method from within the .NET DLL:

JNIEXPORT void JNICALL Java_test_broadcast
(JNIEnv *, jobject)
{
   // Instantiate the MC++ class.
   IManagedWrapper* t = IManagedWrapper::CreateInstance();

   // The actual call is made. 
   t->Broadcast();
}

void ManagedWrapper::Broadcast(std::string message)
{
   //Uncommenting the following line will raise the error
   //IXDBroadcast^ broadcast = XDBroadcast::CreateBroadcast(XDTransportMode::WindowsMessaging);
}

我设法创建了一个.NET DLL,该DLL链接到上述代码并可以按需工作.

I managed to create a .NET DLL that links to the above code and works as desired.

如何从Java代码中调用.NET对象和方法?

How can I call the .NET objects and method from the Java code?

推荐答案

我最终在评论中关注了@"Hovercraft Full Of Eels"链接: 从Java代码调用.Net Dll,而无需使用regasm.exe

I finnaly followed @"Hovercraft Full Of Eels" link in the comments: Calling .Net Dlls from Java code without using regasm.exe

我使用C ++ \ CLI在本机代码和托管代码之间架起了桥梁,并且效果很好. 主要问题是我的桥接DLL在JVM下运行,而我尝试加载的DLL不在JRE \ bin目录中.为克服此问题,我从C ++/CLI代码动态加载了.Net程序集(基于):

I used C++\CLI to bridge between the native and managed code and it worked beautifully. The main issue was that my bridge DLL runs under JVM, and the DLL I tried to load wasn't in JRE\bin directory. To overcome this problem I loaded the .Net assemblies dynamically from C++/CLI code (Based on this):

static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
    //Retrieve the list of referenced assemblies in an array of AssemblyName.
    Assembly^ MyAssembly;
    Assembly^ objExecutingAssemblies;
    String^ strTempAssmbPath = "";

    objExecutingAssemblies = Assembly::GetExecutingAssembly();
    array<AssemblyName ^>^ arrReferencedAssmbNames = objExecutingAssemblies->GetReferencedAssemblies();

    //Loop through the array of referenced assembly names.
    for each (AssemblyName^ strAssmbName in arrReferencedAssmbNames)
    {
        //Check for the assembly names that have raised the "AssemblyResolve" event.
        if (strAssmbName->FullName->Substring(0, strAssmbName->FullName->IndexOf(",")) == args->Name->Substring(0, args->Name->IndexOf(",")))
        {
            //Build the path of the assembly from where it has to be loaded.                
            strTempAssmbPath = pathBase + args->Name->Substring(0, args->Name->IndexOf(",")) + ".dll";
            break;
        }

    }
    //Load the assembly from the specified path.                    
    MyAssembly = Assembly::LoadFrom(strTempAssmbPath);

    //Return the loaded assembly.
    return MyAssembly;
}

这篇关于使用JNI调用第三方.NET DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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