英文:
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
)。
在表示路径的字符串中,你没有对反斜杠进行转义,因此字符串"C:\Users\320035705\Downloads\JNAHelloWorldMWrobel\JNAHelloWorldMWrobel\sampledlls"
最终被解释为"C:Users320035705DownloadsJNAHelloWorldMWrobelJNAHelloWorldMWrobelsampledlls"
。
在Java字符串中,使用两个反斜杠(\\
)表示单个反斜杠。如果你像这样指定路径,对于JNA就应该可以工作:
System.setProperty("jna.library.path",
"C:\\Users\\320035705\\Downloads\\JNAHelloWorldMWrobel\\JNAHelloWorldMWrobel\\sampledlls");
然而,如果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 "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\0035705\\Downloads\\JNAHelloWorldMWrobel\\JNAHelloWorldMWrobel\\sampledlls");
However, if there are issues where the DLL requires another dependency, this could fail for the same reason.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论