使用JNI调用第三方.NET DLL

编程入门 行业动态 更新时间:2024-10-26 15:27:59
本文介绍了使用JNI调用第三方.NET DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从以下位置调用第三方.NET DLL(摘自此处)在JAVA程序中. 在此处和此处我设法将整个内容编译并运行.但是运行.NET代码时出现异常:

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中访问另一个对象和方法时,才会发生这种情况:

This only happens when I try to access another 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

本文发布于:2023-05-27 23:10:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/306078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第三方   JNI   DLL   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!