使用jni4net在Java中使用本机DLL函数,并且出现System.BadImageFormatException错误。

huangapple go评论79阅读模式
英文:

Using jni4net to use Native DLL function in java and getting System.BadImageFormatException

问题

我正在使用 jni4net 在 Java 中使用 DLL 函数。

我使用 jni4net-0.8.6.0-bin,并使用以下命令进行编译:

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

执行此命令时,出现以下错误:

System.BadImageFormatException: 无法加载文件或程序集 'file:///D:\sampledlls\sampledll.dll' 或其依赖项之一。模块应包含程序集清单。

我还尝试使用 JNA 库在 Java 中使用 DLL 函数。但是在那种情况下,我也遇到了以下错误:

java.lang.UnsatisfiedLinkError: 无法加载库 'sampledll':找不到指定的模块。

以下是我的 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\0035705\\Downloads\\JNAHelloWorldMWrobel\\JNAHelloWorldMWrobel\\sampledlls");
        dcmInterfaceDLL sdll = (dcmInterfaceDLL) 
        Native.loadLibrary("sample_interface", dcmInterfaceDLL.class);

        System.loadLibrary("sample_interface");

        sdll.DCM_InitializeFields(); 
    }
}

这是一个 native.dll

我该如何加载我的 DLL?

英文:

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:\Users0035705\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?

答案1

得分: 1

关于jni4net版本,它成功找到了dll文件,但文件格式不正确。有可能你正在使用32位的JVM与64位的DLL,或者反之。你可以尝试在proxygen命令行中添加/32BIT+ /force开关。

DLL可能存在其他问题,因为错误信息提到了它的依赖项。你的评论中提到了与Visual C++运行时程序包相关的错误信息。

<hr>

对于JNA库加载,由于反斜杠没有正确转义,你无法找到DLL文件。

在Java(以及许多其他语言)中,反斜杠(\)是转义字符。在字符串中使用时,它有特殊含义,比如表示换行的(\n)。

在表示路径的字符串中,你没有对反斜杠进行转义,因此字符串&quot;C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls&quot;最终被解释为&quot;C:Users320035705DownloadsJNAHelloWorldMWrobelJNAHelloWorldMWrobelsampledlls&quot;

在Java字符串中,使用两个反斜杠(\\)表示单个反斜杠。如果你像这样指定路径,对于JNA就应该可以工作:

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

然而,如果DLL需要另一个依赖项,由于相同的原因,这可能会失败。

英文:

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.

<hr>

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 &quot;C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls&quot; ends up being interpreted as &quot;C:Users320035705DownloadsJNAHelloWorldMWrobelJNAHelloWorldMWrobelsampledlls&quot;.

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(&quot;jna.library.path&quot;,
    &quot;C:\\Users\0035705\\Downloads\\JNAHelloWorldMWrobel\\JNAHelloWorldMWrobel\\sampledlls&quot;);

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

huangapple
  • 本文由 发表于 2020年8月19日 14:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定