0

I am using jni4net to use the DLL function in Java.

Using jni4net-0.8.6.0-bin I compile using the command:

.\proxygen.exe "D:\sampledlls\sample_interface.dll" -wd "D:\hope"

On executing this command , getting the following error:

System.BadImageFormatException: Could not load file or assembly 'file:///D:\sampledlls\sampledll.dll' or one of its dependencies. The module was expected to contain an assembly manifest.

I also used JNA library to use the DLL function in java. But in that as well I am getting the following error:

java.lang.UnsatisfiedLinkError: Unable to load library 'sampledll': The specified module could not be found.

Here is my code for JNA:

public class hellodll {
    public interface dcmInterfaceDLL extends Library {
        public void DCM_InitializeFields();
    }

    public static void main(String[] args) { 
        System.out.println(System.getProperty("java.library.path")); 
        System.setProperty("jna.library.path",
            "C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls");
        dcmInterfaceDLL sdll = (dcmInterfaceDLL) 
        Native.loadLibrary("sample_interface", dcmInterfaceDLL.class);

        System.loadLibrary("sample_interface");

       sdll.DCM_InitializeFields(); 
    }
}

This is a native.dll.

How can I load my DLL?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63

1 Answers1

1

For the jni4net version, it properly found the dll but it is in the wrong format. It is possible that you are using a 32-bit JVM with a 64-bit DLL, or vice versa. You might try adding /32BIT+ /force switches to the proxygen command line.

There may be other problems with the DLL as the error message mentions its dependencies. Your comments indicate an error message associated with Visual C++ runtime package.


For the JNA library loading, you are not finding the DLL due to improper escaping of backslashes.

In Java (and many other languages) the backslash (\) is an escape character. When used in a String it has special meaning, such as (\n) for a newline.

In your String representing the path, you have not escaped the backslashes, so the String "C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls" ends up being interpreted as "C:Users320035705DownloadsJNAHelloWorldMWrobelJNAHelloWorldMWrobelsampledlls".

Use two backslashes (\\) to represent a single backslash in a Java String. If you specify your path like this, it should work for JNA:

System.setProperty("jna.library.path",
    "C:\\Users\\320035705\\Downloads\\JNAHelloWorldMWrobel\\JNAHelloWorldMWrobel\\sampledlls");

However, if there are issues where the DLL requires another dependency, this could fail for the same reason.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • even after setting it with two backslashes , i am getting the same error. – haritha chandramouli Aug 21 '20 at 06:26
  • It is a 32 bit dll, in the IIS manager i enabled Enable 32-bit application as True, despite that i am getting the error while using jni4net – haritha chandramouli Aug 21 '20 at 06:27
  • i tried to run the dll using the command "rundll32.exe sample_interface.dll,DCM_InitializeFields". getting the error The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail – haritha chandramouli Aug 21 '20 at 06:31
  • That means you need the Visual C++ runtime package. See [this link](https://www.majorgeeks.com/content/page/solved_the_application_has_failed_to_start_because_its_side_by_side_configuration_is_incorrect_error.html) – Daniel Widdis Aug 21 '20 at 06:34
  • that error got resolved. but getting another error : Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls\sample_interface.dll: Can't find dependent libraries – haritha chandramouli Aug 22 '20 at 16:18
  • What are the dependent libraries to the DLL? Are they installed and on the path? – Daniel Widdis Aug 22 '20 at 16:22