java-jna使用

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值