在我接触JNA之前一直以为调用本地链接库函数的时候一定要借助JNI或者SWT来实现,一日中无意看到JNA,发现它也可以实现这类调用,于是下回来按网站上的例子试试了 还不错,因此还有点兴趣了,于是又自己写了 2个发挥了一下。
GetLogicalDriveStringsA【获取本地系统逻辑盘符】
GetSystemDirectoryA 【获取系统目录】
public
interface
Kernel32
extends
Library {
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary( " kernel32 " , Kernel32. class );
int GetLogicalDriveStringsA( int length, byte [] buffer);
int GetSystemDirectoryA( byte [] buffer, int size);
}
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary( " kernel32 " , Kernel32. class );
int GetLogicalDriveStringsA( int length, byte [] buffer);
int GetSystemDirectoryA( byte [] buffer, int size);
}
调用的方式为:
public
class
MainDemo
...
{
public static void main(String[] args) ...{
Kernel32 lib = Kernel32.INSTANCE;
byte[] buffer2=new byte[100];
lib.GetLogicalDriveStringsA(buffer2.length/2,buffer2);
for(byte bt : buffer2)
...{
System.out.print((char)bt);
}
System.out.println();
byte[] buffer = new byte[50];
lib.GetSystemDirectoryA(buffer, 50);
for(byte bt : buffer)
...{
System.out.print((char)bt);
}
}
}
public static void main(String[] args) ...{
Kernel32 lib = Kernel32.INSTANCE;
byte[] buffer2=new byte[100];
lib.GetLogicalDriveStringsA(buffer2.length/2,buffer2);
for(byte bt : buffer2)
...{
System.out.print((char)bt);
}
System.out.println();
byte[] buffer = new byte[50];
lib.GetSystemDirectoryA(buffer, 50);
for(byte bt : buffer)
...{
System.out.print((char)bt);
}
}
}