文章目录
1. jar包下载地址
https://github.com/java-native-access/jna
2. 使用
只需要导入jna和jna-platform,无需将dll文件放在system32中
3. 参考链接
https://blog.csdn.net/weixin_35952352/article/details/118826828
https://wenku.baidu.com/view/85144f2951ea551810a6f524ccbff121dd36c560.html
https://www.bilibili.com/video/av710399207/
https://blog.csdn.net/shenjianxz/article/details/52208889
https://blog.csdn.net/Yalneh/article/details/80905645
https://blog.csdn.net/qq969422014/article/details/53544071
https://blog.csdn.net/Yalneh/article/details/80905645
https://blog.csdn.net/weixin_34018169/article/details/89982906
https://www.qb5200.com/article/251018.html
GetLastError():https://blog.csdn.net/weixin_33963594/article/details/92346226
示范代码
package sample.main;
import java.util.HashMap;
import java.util.Map;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;
public class test {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.load((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
// kernel32.dll uses the __stdcall calling convention (check the function
// declaration for "WINAPI" or "PASCAL"), so extend StdCallLibrary
// Most C libraries will just extend com.sun.jna.Library,
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE1 = (Kernel32)
Native.load("kernel32", Kernel32.class);
// Optional: wraps every call to the native library in a
// synchronized block, limiting native calls to one at a time
Kernel32 SYNC_INSTANCE = (Kernel32)
Native.synchronizedLibrary(INSTANCE1);
@Structure.FieldOrder({
"wYear", "wMonth", "wDayOfWeek", "wDay", "wHour", "wMinute", "wSecond", "wMilliseconds"})
public static class SYSTEMTIME extends Structure {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
void GetSystemTime(SYSTEMTIME result);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello,22 World\n");
for (int i = 0; i < args.length; i++) {
System.out.println(111);
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
Kernel32 lib = Kernel32.INSTANCE1;
Kernel32.SYSTEMTIME time = new Kernel32.SYSTEMTIME();
// lib.GetSystemTime(time);
System.out.println("Today's integer value is " + time.wDay);
}
}