使用JNI调用第三方.NET DLL

 2023-01-20    307  

问题描述

我正在尝试致电第三方.net dll(从 shere 中获取)在Java程序中.
在寻找在这里 =” http://www.codeproject.com/articles/2876/jni-basics-1/?fid=4729&df=90&mpp = 50&noise = 3&prof = false&sort&sort = ppition&view = = = = quick&fr = quick&fr = 51#xx0xxxxxxx x x x x 我设法使整个事情进行编译和运行.但是在运行.NET代码时我会有例外:

Java运行时环境已检测到致命错误

使用JNI调用第三方.NET DLL

这仅在我尝试从.NET DLL中访问另一个.NET对象和方法时才发生.

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链接到上述代码并根据需要工作.

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

推荐答案

i Finnaly在评论中遵循 @throughnaly @through @
无需使用Java Code而不使用Java dlls regasm.exe

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

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;
}

其他推荐答案

这正是本机Java到.NET Bridges在后台所做的.

取决于.NET代码的数量,您需要使用和复杂的.NET对象(如果使用仿制药,如果您有数组等等,则使用哪种方法.当您深入研究这种情况时,如果您自己通过C ++/CLI手动进行操作,则会出现更多的限制.如果您需要快速可靠的生产环境解决方案,我建议检查:

  • javonet
  • jnbridge
  • jni4net
  • 这些桥梁中的每一个都将覆盖您的所有本机通信,并为您提供非常简单的接口.使用任何DLL,甚至使用Java应用程序中的整个.NET框架.

    根据您的需求, javonet 我认为是最简单,非常轻巧的功能和灵活性的,如果您寻找具有良好支持和易于使用的API的可靠商业解决方案,请确定有用在5分钟内(对于非商业和学术是免费的). jnbridge 更加强烈的代理类别生成器也非常强大,并且对商业使用也有好处,尤其是如果您需要任何专用连接器(会影响价格,但取决于您的需求). jni4net 我想说的是非关键,非商业补充的好开源项目,但值得检查.

    使用此类桥梁,您不在乎任何其他实现,只需复制.NET DLL并在Java中使用它.您可以在这里看到: http://www.youtube.com/watch?v=n6xfzrhtdk4/a>

    以上所述是小编给大家介绍的使用JNI调用第三方.NET DLL,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

    原文链接:https://77isp.com/post/26115.html

    =========================================

    https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。