Java .NET interop

Well, I delved into this for a while, and I'm looking for an entrance.

I need a Java application that can load and unload its own libraries, quite simply, C / C ++ / Java / Scripts / Executables / etc. not a real issue using JNI and other built-in functions.

However, I need the ability to also download .NET libraries, which was a crazy disappointment. My first attept was to use JNI and call the C ++ shell, as shown below:

Java:

this.Lib = (LibHandler)Native.loadLibrary("MyLib", LibHandler.class);

CPP:

#include <jni.h>
#using <MyLibCSharp.dll>
#include <vcclr.h>
#include <msclr\marshal.h>

using namespace msclr::interop;
using namespace System::Runtime::InteropServices;
using namespace System;

extern "C" 
{
    JNIEXPORT jstring JNICALL Java_Test(JNIEnv * env)
    { 
        marshal_context ^ context = gcnew marshal_context();
        const char* str4 = context->marshal_as<const char*>(CSharp::Class1::Test());

        jstring js = env->NewStringUTF(str4);


        return js;
    }

    JNIEXPORT jstring JNICALL Java_Test2(JNIEnv * env, jobject jobj)
    { 
        marshal_context ^ context = gcnew marshal_context();
        const char* str4 = context->marshal_as<const char*>(CSharp::Class1::Test());

        jstring js = env->NewStringUTF(str4);


        return js;
    }
}

, , MyLib.dll # one, ( , C , , .NET , , ++), .

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'MyLib.dll': The specified module could not be found.
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:163)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:236)
    at com.sun.jna.Library$Handler.<init>(Library.java:140)
    at com.sun.jna.Native.loadLibrary(Native.java:379)
    at com.sun.jna.Native.loadLibrary(Native.java:364)
    at EntryPoint.main(EntryPoint.java:31)

, # COM- , :

ActiveXComponent comp = new ActiveXComponent("MyLib.Class1");

:

Exception in thread "main" com.jacob.com.ComFailException: Can't co-create object
    at com.jacob.com.Dispatch.createInstanceNative(Native Method)
    at com.jacob.com.Dispatch.<init>(Dispatch.java:99)
    at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58)
    at EntryPoint.main(EntryPoint.java:33)

#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;



namespace MyLib
{
    [Guid("4F3A0A13-4D2B-4DE6-93EA-D6861C230290"),
    ComVisible(true)]
    public interface ITest
    {
        [DispId(1)]
        string Test();
    }

    [Guid("A78C5820-3E4B-49B3-8C8E-63DD12346410"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    ComVisible(true)]
    public interface ITestEvents
    {
    }

    [Guid("3ECD46AE-E8F4-4B62-B9DC-DD7F6CB435E2"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(ITestEvents)),
    ComVisible(true)]
    public class Class1 : ITest
    {
        public string Test()
        {
            return "This is my C# DLL COM returning a string! heh!";
        }
    }
}

, COM , oleview.exe, vbScript... , , .

COM, , ( , C/++ VB6).

CLI ++, , .

, .

Edit:

System.LoadLibrary, , Native.LoadLibrary .

+5
4

JNI ++, ++ 32-, 64- JVM, .

.NET( GAC), / Java, , Java, .

, , JNI ↔ .NET ( , ).

+6

, , . JNI ++/CLI, .NET , ​​ COM-.

, , , ref/out, , , .NET, , ..... , , .

, , Java .NET Bridges, :

, IKVM. .NET JAVA. / , .

JNBridge - . , BizTalk. , Javonet - jar , API. , Javonet -, - , .NET , JNBridge -, , , , , , , Javonet , .

, , , .NET JAVA . - - - . 30% , .NET( ).

, , , Java .NET.

Javonet .NET Random ( , DLL, .NET):

public void GenerateRandomNumber() throws JavonetException 
{
        NObject objRandom = Javonet.New("System.Random");
        int value = objRandom.invoke("Next",10,20);

        System.out.println(value); 
}
+5

, , IKVM , Java- .Net:

IKVM.NET is a Java implementation for Mono and Microsoft.NET. Framework It includes the following components:

* A Java Virtual Machine implemented in .NET
* A .NET implementation of the Java class libraries
* Tools that enable Java and .NET interoperability
+4
source

Your COM interaction code looks almost correct. However, you cannot instantiate a class with a late constraint until you define the default interface for the class as follows:

[Guid("3ECD46AE-E8F4-4B62-B9DC-DD7F6CB435E2"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(ITest)),
ComSourceInterfaces(typeof(ITestEvents)),
ComVisible(true)]
public class Class1 : ITest
+1
source

All Articles