0

I have to access VBA-code of Office applications from my Java application, I found THIS that says I can access VBA-code through VisualBasic DLLs using JNI. I don't want to use a COM-bridge if not necessary, I'd rather go with a DLL-solution. I created a VisualBasic Class Library in Visual Studio 2013 (a simple example to test if it works):

Public Class Test1
    Public Function box()
        MsgBox("boxtest!")
    End Function
End Class

I built is as a release and put this in my Java project:

public class Test1 {
    static{
        System.loadLibrary("Test1");
    }
    public native void box(); 
}

The function is being called by new Test1().box();.

I receive the following exception: Exception in thread "main" java.lang.UnsatisfiedLinkError: test.Test1.box()V

I also used JNA to access the DLL but after hours of trying I couldn't get it to work (I also read that it can't be used with VisualBasic DLLs). I set the Native Library Folder of my src Folder to to folder containing the DLL.

question: Can I use VisualBasic DLLs in Java, if yes, with JNA or JNI (or both) and if so what did I do wrong, how can I access the function properly? (I guess the rest with returning and parameters is easy then...)

Thank you very much in advance and merry christmas to you all! :)

Community
  • 1
  • 1
  • Ok, so we've got web 2.0 now I guess we need PC 2.0! What with the .net framework it should be easy to call any library from any major language... btw .net uses CLI, so by default VS C++ code may not work the same as code from other "standard" compilers. – Jason K. Mar 20 '17 at 20:31

2 Answers2

1

No idea why it cannot find your library. From what I remember of JNI, it does not appear that you've done the JNI setup for calling a native routine, but the error message just says it cannot find it. You might try figuring out if the library load statement worked.

A DLL is a library following certain rules and conventions; I am not aware of any great difference between a "Visual Basic DLL" and any other kind. At some level they need to be the same, because Windows programs don't distinguish among DLLs written with different languages, afaik, and I've done VB enough to know that I haven't seen documentation that says "this can be used from VB but not from other languages" etc.

Getting JNI/JNA stuff to work is tricky and tedious. The normal stuff that a language runtime tells you, especially a Java runtime, are not there for you in this case. You must painstakingly go through every line of whatever documentation you have, every parameter you are passing, every use of value versus reference, etc.

I once got things to work with the GitHub library here.

Good luck.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thanks for your answer! The `System.loadLibrary([...])` worked, it just can't find the method inside the library. After some days of research I have not found a single thing on how to call methods in VB-DLLs. Although I found things on calling C++ DLLs (and so VB), there are several examples for calling VB DLLs through a C++ wrapper. –  Dec 27 '14 at 17:45
0

I have not found an answer how to call VB DLLs directly from Java but after some days of research I found out that you are able to call VB DLLs with the help of a C++ wrapper.

It may be possible to call VB DLL methods with JNI but there is no documentation on how to do it. You find a lot about how to create C++ libraries that are able to communicate with JNI in the JNI specifications from Oracle.

In this special case (control Office Applications with Java) I suggest to write the code to access the Office Application in C++ and create a DLL.

The basic approach on creating a C++ DLL that may interact with JNI is:

  1. Think of names for the methods you want to create in C++ and a .dll-name [NAME].dll

  2. Create a Java class for the DLL, loading the library:

    static{
      System.loadLibrary([NAME].dll);
    }
    

    The Native Library path has to be set (in Eclipse, right-click on the folder containing your class and click Build-Path).

  3. Include the method names public native void [methodname]();.

  4. Compile the .java file using javac.exe (or let for example Eclipse do the work).

  5. Create a C++ header-file using javah.exe with -jni parameter.

  6. Create a new project in Visual Studio (Visual C++ MFC DLL).

  7. Copy the created header-file (your Java Project), the jni.h (JDK) and the jni_md.h (JDK)

  8. Include all three header-files in your Visual C++-project header-file [Project-Name].h

  9. Include the created header-file and the jni.h-file in [Project-name].cpp.

  10. Write desired code in your [Project-name].cpp.

  11. Build the DLL, put it inside your defined path for Native Libraries (see italic in 2.).

  12. Run and be happy!

Sorry for any mistakes!

An example with Visual Basic-DLL and JNI can be found 1HERE and somewhere else, google "classle" and "JNI" (can't post 2 links).