Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in accessing dll file from a Java program through JNA

Tags:

java

jna

I have a dll file and I am trying to call functions of it through a Java program through JNA

But the problem is It is not able to locate my dll file and throwing the following exception:

java.lang.UnsatisfiedLinkError: Unable to load library 'UsbDll': 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.NativeLibrary.getInstance(NativeLibrary.java:199)
at com.sun.jna.Native.register(Native.java:1018)
at com.MainClass.<clinit>(MainClass.java:15)
Exception in thread "main"  

Below is my program:

package com;

import com.sun.jna.Native

public class MainClass {

    static {
        Native.register("UsbDll");
    }

public native int method();

    public static void main(String[] args) {
    }
}

The name of my dll file is UsbDll.dll and my operating system is Windows.

============================ EDITED ================================

The location of my dll file is "c:\UsbDll.dll"

When I placed another dll file at the same location, JNA has located it so I think that the problem is with my "UsbDll.dll" file only.

When I tried to load both the dll files (UsbDll.dll and the another dll) with the following command

System.load("c:\\UsbDll.dll");
System.load("c:\\another.dll");

It loaded the "another.dll" successfully but for "UsbDll.dll", it throws the following exception:

java.lang.UnsatisfiedLinkError: C:\UsbDll.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1699)
at java.lang.Runtime.load0(Runtime.java:770)
at java.lang.System.load(System.java:1003)
at com.MainClass.<clinit>(MainClass.java:16)

Exception in thread "main" 

Q1. It looks like it is not finding some dependent libraries. But as I am totally new to these dlls, Is there anything I am missing or I need to ask the vendor to provide me the dependent libraries.

OR

Is it depends on some standard libraries which I can download from internet? If it is, then how to find the libraries name on which it depends?

============================ EDITED #2 ================================

I ran the Dependency Walker on my UsbDll.dll file to find the missing dependencies and found the following missing dependencies.

WER.DLL (referred by library WINNM.DLL found in my c:\windows\system32)

IESHIMS.DLL (referred by library WINNM.DLL found in my c:\windows\system32)

WDAPI920.DLL (referred directly by my UsbDll.dll)

Q1. As WINNM.DLL was found in my system32 folder, it seems as the standard dll. And if this standard dll is referring to 2 missing dlls (WER.DLL & IESHIMS.DLL), then I suspect how other applications are working who are using this WINNM.DLL file?

Q2. I googled for WDAPI920.dll (that was referred my UsbDll.dll directly) and many search results appeared with the same dll name. So it also looks like some standard library. So how to fix these dependencies? From where to download them? After downloading, Can I place them in the same directory in which my main dll (UsbDll.dll) is or I need to do something extra to load my dll (UsbDll.dll) sucessfully?

like image 534
Yatendra Avatar asked Jan 26 '11 14:01

Yatendra


People also ask

Can we use DLL in Java?

To use an arbitrary DLL from Java you usually have to create an adapting DLL with the conventions of JNI that itself loads the "target" DLL and calls the required functions. To generate the correct headers for your adapter DLL you can use the tool javah shipped with the JDK.

How do I invoke an external DLL library method function from Java code?

You will need to use the Java Native Interface (JNI), which is a set of C/C++ functions that allow native code to interface with java code (i.e. receiving parameters from java function calls, returning results, etc). Write a wrapper C library that receive JNI calls and then call your external library.

What is DLL file in Java?

Dynamic Link Library (DLL) is Microsoft's implementation of the shared library concept. A DLL file contains code and data that can be used by multiple programs at the same time, hence it promotes code reuse and modularization. This brief tutorial provides an overview of Windows DLL along with its usage.


1 Answers

From your edited code it is quite evident that the UsbDll.dll depends on some standard modules which are not there on your system (for example if it uses ATL and if you have don't have proper runtime then it is guaranteed to fail). To resolve this you will need proper runtime environment.

Also it is possible that the dll in concern depends on some vendor specific module. For you the best option is (and fastest option would be) to contact the vendor. Otherwise, try to install proper runtime from the microsoft site (but its more of hit-and-trial)


Update

Use the below links for finding more about DLL dependency:

  1. How do I determine the dependencies of a .NET application?
  2. http://msdn.microsoft.com/en-us/library/ms235265.aspx
  3. Command line tool to find Dll dependencies


Update 2

See the below mentioned link for the missing dll details (but it is specific to the windows version)

  1. Dependency Walker reports IESHIMS.DLL and WER.DLL missing?
  2. http://social.msdn.microsoft.com/Forums/en/vsx/thread/6bb7dcaf-6385-4d24-b2c3-ce7e3547e68b

From few simple google queries, WDAPIXXX.dll appears to be some win driver related thing (although i am not too sure). Check this link, they have something to say about WDAPI http://www.jungo.com/st/support/tech_docs/td131.html.

like image 73
Favonius Avatar answered Oct 04 '22 01:10

Favonius