1. 描述备注

参考地址1

JNA示例代码

1.1 JNA工作原理

	JNA是建立在JNI技术基础之上的一个Java类库,它使您可以方便地使用java直接访问动态链接库中的函数。
原来使用JNI,你必须手工用C写一个动态链接库,在C语言中映射Java的数据类型。
JNA中,它提供了一个动态的C语言编写的转发器,可以自动实现Java和C的数据类型映射。你不再需要编写C动态链接库。 JNA把一个dll/.so文件看做是一个java接口。Dll是C函数的集合、容器,这和java接口的概念吻合。

1.2 Java和C数据类型对应表

Java Type C Type Native Representation
boolean int 32-bit integer (customizable)
byte char 8-bit integer
char wchar_t platform-dependent
short short 16 bit integer
int int 32 bit integer
long long long,_int64 64 bit integer
float float 32-bit floating point
double double 364-bit floating point
Buffer Pointer pointer platform-dependent (32- or 64-bit pointer to memory)
[] (array of primitive type) 32- or 64-bit pointer to memory (argument/return) contiguous memory (struct member)
String char* NUL-terminated array (native encoding or jna.encoding)
WString wchar* NUL-terminated array (unicode)
String[] char** NULL-terminated array of C strings
WString[] wchar** NULL-terminated array of wide C strings
Structure struct* struct pointer to struct (argument or return) (or explicitly) struct by value (member of struct) (or explicitly)
Union union same as Structure
Structure[] struct[] array of structs, contiguous in memory
Callback (*fp)() function pointer (Java or native)
NativeMapped varies depends on definition
NativeLong long platform-dependent (32- or 64-bit integer)
PointerType pointer same as Pointer

1.5 POM.xml

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.5.0</version>
</dependency>

2. 示例

2.1示例1-普通调用

public class HelloWorld {

	//定义一个接口,继承自Library或stdCallLibrary,默认的是继承Library ,如果动态链接库里的函数是以stdcall方式输出的,那么就继承StdCallLibrary,比如众所周知的kernel32库。
public interface CLibrary extends Library { //接口内部需要一个公共静态常量:INSTANCE,通过这个常量,就可以获得这个接口的实例,从而使用接口的方法,也就是调用外部dll/so的函数。
//通过Native.loadLibrary() API获取,
//第一个参数是动态链接库dll/so的名称,不带后缀。搜索动态链接库路径的顺序是:当前类的目录-->工程文件夹下面找win32/win64文件夹下搜
-->WINDOWS下面去搜索,再找不到就会抛异常了。比如上例中printf函数在Windows平台下所在的dll库名称是msvcrt,而在其它平台如Linux下的so库名称是c。
//第二个参数是本接口的Class类型。JNA通过这个Class类型,根据指定的.dll/.so文件,动态创建接口的实例。该实例由JNA通过反射自动生成
CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),CLibrary.class); //接口中只需要定义你要用到的函数或者公共变量,不需要的可以不定义,如上例只定义printf函数, 入参和出参和dll中定义保持一致
void printf(String format, Object... args);
} public static void main(String[] args) { //直接调用即可
CLibrary.INSTANCE.printf("Hello, World\n"); for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}

2.2 示例2-调用win32DLL(JNA已封装)

//获取系统时间
Kernel32 lib = Kernel32.INSTANCE;
SYSTEMTIME time = new SYSTEMTIME();
lib.GetSystemTime(time); System.out.println("Today's integer value is " + time.wDay);

示例3-Win32API 操作进程

Kernel32 lib = Kernel32.INSTANCE;

// 获取进程快照
HANDLE hSnapshot = lib.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new DWORD(0)); // 获取进程
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
// lib.Process32First(hSnapshot, lppe); while (lib.Process32Next(hSnapshot, pe32)) {
DWORD processId = pe32.th32ProcessID;
char[] name = pe32.szExeFile;
System.out.println("进程id: " + processId + " ; " + "进程名称: " + new String(name)); // 打开进程
HANDLE processHandle = lib.OpenProcess(WinNT.PROCESS_ALL_ACCESS, false, pe32.th32ProcessID.intValue());
}

JNA-调用win32 Dll文件的更多相关文章

  1. 在 C# 中通过 P/Invoke 调用Win32 DLL

    在 C# 中通过 P/Invoke 调用Win32 DLL 发布日期 : 1/13/2005 | 更新日期 : 1/13/2005 Jason Clark 下载本文的代码: NET0307.exe ( ...

  2. Atitit.java jna 调用c++ dll 的总结

    Atitit.java jna 调用c++ dll 的总结 1. JNA技术解密1 1.1. JNA工作原理1 2. JNA技术难点 Java—C和操作系统数据类型的对应表1 2.1. 1 2.2.  ...

  3. Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()

    Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...

  4. C# 调用win32 DLL报错 System.BadImageFormatException

    C# 调用win32 DLL报错  System.BadImageFormatException 项目右键属性->项目设计器->生成->平台->把'默认设置(任何 CPU)'改 ...

  5. C#调用C++ DLL 文件

    说来惭愧,都注册一年多了,却没有发表过一篇正式的博文,中间很多学习的过程也没有记录下来.如今到了一个新的环境,也有了学习的机会,一定要把每天的收获记录一下. 要做的东西需要引用C++编写的DLL,刚开 ...

  6. 对C#调用C++ dll文件进行总结

    在实际项目工作中,经常用到C#调用C++ 或者C编写的dll文件. dll支持一般函数声明和类的定义声明,但是一般为了简化,都是 采用函数声明的方式.这里主要并不是写 dll的编写. 先在vs中创建一 ...

  7. java调用c#dll文件配置

    1 在强大的c#语言和java语言之间,二者难免会因为某些特殊的要求会相互调用. 下面就以java调用c#的dll为例做详细介绍 1  在vs中的环境设置如下图,图片中程序仅作为讲解程序,在项目编译成 ...

  8. VS2010 C#调用C++ DLL文件 【转】

    http://www.soaspx.com/dotnet/csharp/csharp_20110406_7469.html 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第 ...

  9. 用vc生成可被python调用的dll文件

    前提已经有.c 和.i文件 用swid编译了.i文件生成了wrap.c文件和.py文件 vc创建dll工程 将.h加入到头文件中.c文件和wrap.c文件添加到源文件中 将.i文件添加到工程目录下To ...

随机推荐

  1. 基于ftp服务实现yum网络共享

    安装ftp服务:yum install vsftpd 安装后: CentOS7 启动服务:systemctl start vsftpd 设置开机启动:systemctl enable vsftpd 同 ...

  2. html锚链接

    锚点(anchor):其实就是超链接的一种,一种特殊的超链接 普通的超链接,<a href="路径"></a> 是跳转到不同的页面 而锚点,<a hr ...

  3. 【java并发编程艺术学习】(三)第二章 java并发机制的底层实现原理 学习记录(一) volatile

    章节介绍 这一章节主要学习java并发机制的底层实现原理.主要学习volatile.synchronized和原子操作的实现原理.Java中的大部分容器和框架都依赖于此. Java代码 ==经过编译= ...

  4. k8s 基础 问题

    vim /usr/lib/systemd/system/docker.service --insecure-registry registry.access.redhat.com \ ubelet.s ...

  5. 杭电acm 1034题

    Problem Description A number of students sit in a circle facing their teacher in the center. Each st ...

  6. p2921 Trick or Treat on the Farm

    传送门 题目 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果.农场不大,所以约翰要想尽法子让奶牛们得到快乐 ...

  7. 1.8 收集的XSS Payload

    收集的XSS Payload ,可以做成字典,到时候批量测试:--------------------------------------------------------------------- ...

  8. java笔记--关于线程同步(5种同步方式)

    转自:http://www.2cto.com/kf/201408/324061.html 为何要使用同步?     java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改 ...

  9. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  10. DES的雪崩效应分析

    明文固定,密钥改变一个字节 假定明文为11111111(00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001) ...