从现有外部 .dll 调用方法。例如,来自 kernel32.dll 的 CopyFileA
任务是从现有的 dll 中调用方法。 我正在尝试对 kernel32.dll 中的 CopyFileA 示例执行此操作。
方法签名是:
Function long CopyFileA(String lpExistingFileName, String lpNewFileName, boolean bFailifExists) Library "kernel32"
这就是我在 java 中尝试执行此操作的方式:
public class Test {
static {
System.loadLibrary("D:\\test\\kernel32");
}
public static void main(String[] args) {
(new Test()).CopyFileA("D:\\test\\hi.txt", "D:\\other\\hi.txt", false);
}
public native long CopyFileA(String lpExistingFileName, String lpNewFileName, boolean bFailifExists);
}
我得到:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.CopyFileA(Ljava/lang/String;Ljava/lang/String;Z)J
我找到的所有手册都描述了编写 C 代码然后为自己创建 dll 时的示例。因此,您可以使用生成的头文件中的签名来实现本机方法。 但这里我们已经有了一个dll。
谢谢!
The task is to call method from an existing dll.
I'm trying to do that on an example of CopyFileA from kernel32.dll.
The method signature is:
Function long CopyFileA(String lpExistingFileName, String lpNewFileName, boolean bFailifExists) Library "kernel32"
This is how I'm trying to do this in java:
public class Test {
static {
System.loadLibrary("D:\\test\\kernel32");
}
public static void main(String[] args) {
(new Test()).CopyFileA("D:\\test\\hi.txt", "D:\\other\\hi.txt", false);
}
public native long CopyFileA(String lpExistingFileName, String lpNewFileName, boolean bFailifExists);
}
I'm getting:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.CopyFileA(Ljava/lang/String;Ljava/lang/String;Z)J
All manuals that I've found describes examples when you write C code and then create dll for yourself. So, you implement native method with signature from generated header file.
But here we already have a dll.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的示例是最好的方法。需要完成一些利用代码才能使 Java 能够调用本机方法,反之亦然。如果没有这个线束代码,它们中的任何一个都无法相互通信。
如果您迫切希望调用 CopyFileA,请在一些 C/C++ 代码中创建线束代码,然后调用 CopyFileA。
如果您试图避免使用 C/C++ 进行编程,那么您的 java 就无法与 CopyFileA 进行通信。
可能有第三方代码可以帮助您。我什么都不知道。
The examples you have seen are the best way to go. There is some harness code that needs to be done to enable Java to call into a native method and visa-versa. With out this harness code there is no way for either of them to communicate with each other.
If you are desperate to call CopyFileA then create the harness code in some C/C++ code that then calls CopyFileA.
If you are trying to avoid programming in C/C++ then there is no way for your java to communicate with CopyFileA.
There may be a third party code that may help you. I don't know of any.
这非常简单:您需要的只是下载 jna.jar 文件并将其包含到您的项目中。
下面我放了一些代码片段如何解决您的任务:
This is really simple: everything you need is download jna.jar file and include it into your project.
Bellow I put some code snippet how to solve your task: