Search code examples
java.netexcelikvmvba

How do I make a .dll created with IKVM com visible?


I have seen a few posts on this, but I haven't seen any solutions so far. I have a .jar file that I'm converting to a .NET DLL via IKVM. I'm trying to figure out how to make the methods in the DLL available inside the excel VBA environment. here are the details.

1.) installed IKVM & registered it's DLL's to GAC

2.) ran IKVM to create the a .net .dll (mytest.dll)

ikvmc mytest.jar

3.) registered the new .dll

regasm mytest.dll

4.) From here i created a VB.NET project and added mytest.dll and IKVM.OpenJDK.Core.dll as references to the project. I am then able to access the methods within the .dll in .NET. This is great!

5.) what I really want to do is be able to use the .dll in VBA as well. Initially vba wouldn't accept the .dll directly as it's a .net library. I attempted to create a type library:

regasm /codebase /tlb mytest.dll

This created a .tlb file which is nice, but it did throw a warning about the library not being strongly named.

6.) then I loaded the .tlb as a reference in my vba editor. This works, however when I try to access the methods nothing shows up. Similarly if I look in the object viewer for my library i can see my two classes but not the members of those classes.

Additionally, I imagine that I probably also need to somehow reference the IKVM.OpenJDK.Core.dll inside VBA as well. However I can't do that either since it's a .NET .dll.

Has anyone had success converting a .jar file into something that can be used with VBA?


Solution

  • I think you always need to explicitly mark a class to be usable via COM interop. Here's an example of a Java class that is usable from VBA:

    import cli.System.Runtime.InteropServices.*;
    
    @ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
    public class SampleWidget {
      public int Add(int x, int y) {
        return x + y;
      }
    }
    

    Here are the steps to compile:

    1. Copy IKVM.Runtime.dll and all IKVM.OpenJDK.*.dll into the current directory or the GAC.
    2. Run "ikvmstub mscorlib" to generate mscorlib.jar.
    3. Create a Java source named SampleWidget.java containing the code above.
    4. javac -cp mscorlib.jar;. SampleWidget.java
    5. ikvmc -out:SampleLibrary.dll SampleWidget.class -r:mscorlib.dll
    6. tlbexp SampleLibrary.dll
    7. regasm /codebase SampleLibrary.dll (this step needs administrator rights)

    Now you can add a reference to the SampleLibrary.tlb from VBA and use the SampleWidget class.