我正在尝试CreateFile
使用 JNA 在 Windows 7 上调用 Win32 的函数,目的是对该答案进行 Java 实现,以检查文件是否正在被另一个进程使用。
我到目前为止的代码是:
import com.sun.jna.Native;
import com.sun.jna.examples.win32.Kernel32;
public class CreateFileExample {
static int GENERIC_ACCESS = 268435456;
static int EXCLUSIVE_ACCESS = 0;
static int OPEN_EXISTING = 3;
public static void main(String[] args) {
Kernel32 kernel32 =
(Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
kernel32.CreateFile("c:\\file.txt", GENERIC_ACCESS, EXCLUSIVE_ACCESS,
null, OPEN_EXISTING, 0, null);
}
}
但是,运行它会引发异常:
java.lang.UnsatisfiedLinkError: Error looking up function 'CreateFile': The specified procedure could not be found.
如果我"kernel32"
将loadLibrary
调用更改为无效的东西,那么我会得到The specified module could not be found
这样的结果,这表明从库路径中正确找到了 DLL,但是我调用的方式有问题CreateFile
。
任何想法我做错了什么?
CreateFile
定义com.sun.jna.examples.win32.Kernel32
为:
public abstract com.sun.jna.examples.win32.W32API.HANDLE CreateFile(
java.lang.String arg0,
int arg1,
int arg2,
com.sun.jna.examples.win32.Kernel32.SECURITY_ATTRIBUTES arg3,
int arg4,
int arg5,
com.sun.jna.examples.win32.W32API.HANDLE arg6);