0

I am new to using JNA.In all what i want to do is use the vb DLL file in java & call the functions from java. I created a simple java code for this.

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class Main
{
    public interface test extends Library
    {
        void fn_Today(int a,int b);
    }

    public static void main(String[] args)
    {
        test INSTANCE = (test) Native.loadLibrary(
            (Platform.isWindows() ? "test" : "test"), test.class);

        int a = 1;
        int b=2;

        INSTANCE .fn_Today(a,b); 
    }
}

When i run this java program i am getting following error-

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up      function 'fn_Today': The specified procedure could not be found.

at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:344)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:324)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at com.sun.proxy.$Proxy0.fn_Today(Unknown Source)
at mwrobel.jna.Main.main(Main.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Dll code is as

Public Function fn_Today(ival As Integer, ival2 As Integer)
Dim ret As Integer
ret = ival + ival2

End Function

How can i solved this issue?

i got the following output from dependency walker.

Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing  
export function in a delay-load dependent module.
Suresh A
  • 1,178
  • 2
  • 10
  • 21
  • This might be applicable: http://stackoverflow.com/questions/30565496/jna-couldnt-find-the-specified-procedure-in-dll-file-through-java – theduck Jun 02 '15 at 07:30
  • And this one: http://stackoverflow.com/questions/22695829/call-dll-from-java-using-jna – theduck Jun 02 '15 at 07:32
  • VB DLLs use the `stdcall` calling convention, so at the very least you need to implement `StdCallLibrary` instead of `Library`. – technomage Jun 02 '15 at 13:08
  • You likely also need to use a `StdCallFunctionMapper` to decorate function names according to stdcall conventions. – technomage Jun 02 '15 at 13:09
  • BTW, running [Dependency Walker](http://dependencywalker.com) on your DLL will show you the actual labels exported for your functions. See also the [JNA FAQ](https://github.com/twall/jna/blob/master/www/FrequentlyAskedQuestions.md#my-library-mapping-causes-an-unsatisfiedlinkerror). – technomage Jun 02 '15 at 15:45
  • You also need to determine if your DLL is a dotNET assembly or a straight up C-compatible shared library (DLL). – technomage Jun 02 '15 at 15:56
  • could you please tell me how to use StdCallFunctionMapper function in my above code? @technomage – Suresh A Jun 03 '15 at 05:31

1 Answers1

1

If your VB DLL is a C-compatible shared library (if you run Dependency Walker on it you'll see labels of the form XXX@NNN), then the following should work.

import com.sun.jna.win32.StdCallFunctionMapper;
import com.sun.jna.win32.StdCallLibrary;

public class Main
{
    public interface TestLibrary extends StdCallLibrary
    {
        void fn_Today(int a,int b);
    }

    public static void main(String[] args)
    {
        Map options = new HashMap() {
            { put(Library.OPTION_FUNCTION_MAPPER, new StdCallFunctionMapper()) }
        };
        TestLibrary INSTANCE = (TestLibrary) Native.loadLibrary("test", TestLibrary.class, options);

        // ...
    }
}
technomage
  • 9,861
  • 2
  • 26
  • 40
  • i am still getting this error. I think problem is in dll .How do i know VB DLL is a C-compatible shared library or not? @technomage – Suresh A Jun 04 '15 at 05:44
  • Include the relevant output from [DependencyWalker](http://dependencywalker.com) in your question. You should also add an update to your question which reflects the changes you've made thus far. – technomage Jun 04 '15 at 23:56
  • i did add an update in my question .See and give me response @technomage – Suresh A Jun 07 '15 at 07:18
  • Did you update your code to match the example in the answer? What did symbols does dependency walker show are in your DLL? – technomage Jun 08 '15 at 12:41
  • i got Warning: At least one delay-load dependency module was not found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module. – Suresh A Jun 09 '15 at 05:02