JNA技术

JNA来自https://github.com/java-native-access/jna

已经编译好的为https://maven.java.net/content/repositories/releases/net/java/dev/jna/jna/4.2.1/jna-4.2.1.jar

我们以jna-4.2.1的版本为例进行研究

alphamaskdemo: 一个简单演示Windows.setAlpha的功能。需要jna-4.2.1和jna-platform-4.2.1

BalloonManagerDemo:  这个演示WindowsUtils的setWindowMask和setWindowAlpha的用法。

monitordemo: 这个读取显示器的信息, 用这个User32.INSTANCE.EnumDisplayMonitors读取显示器的信息

MONITORINFOEX info = new MONITORINFOEX();

User32.INSTANCE.GetMonitorInfo(hMonitor, info);

读取显示器信息

shapedwindowdemo: 一个时钟窗口

 

.NET平台上强大的P/Invoke

而在.NET平台上,强大的P/Invoke技术使我们Java程序员非常羡慕。使用P/Invoke技术,只需要使用编写一个.NET函数,再加上一个声明的标注,就可以直接调用dll中的函数。

不需要你再使用C语言编写dll来适配。

 

不逊于P/InvokeJNA

现在,不需要再羡慕.NET的P/Invoke机制了。JNA把对dll/.so共享库的调用减少到了和P/Invoke相同的程度。

 

使用JNA,不需要再编写适配用的.dll/.so,只需要在Java中编写一个接口和一些代码,作为.dll/.so的代理,就可以在Java程序中调用dll/so。

 

JNA工作原理

JNA是建立在JNI技术基础之上的一个Java类库,它使您可以方便地使用java直接访问动态链接库中的函数。

原来使用JNI,你必须手工用C写一个动态链接库,在C语言中映射Java的数据类型。

JNA中,它提供了一个动态的C语言编写的转发器,可以自动实现Java和C的数据类型映射。你不再需要编写C动态链接库。

JavaC和操作系统数据类型的对应表

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

64-bit floating point

Buffer

Pointer

pointer

platform-dependent (32- or 64-bit pointer to memory)

<T>[] (array of primitive type)

pointer

array

32- or 64-bit pointer to memory (argument/return)

contiguous memory (struct member)

除了上面的类型,JNA还支持常见的数据类型的映射。

 

 

String

char*

NUL-terminated array (native encoding or jna.encoding)

WString

wchar_t*

NUL-terminated array (unicode)

String[]

char**

NULL-terminated array of C strings

WString[]

wchar_t**

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

<T> (*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

 

JNA编程过程

 

JNA把一个dll/.so文件看做是一个Java接口。

Dll是C函数的集合、容器,这正和接口的概念吻合。

 

    我们定义这样一个接口,

public interface TestDll1 extends Library {

                   /**

                    * 当前路径是在项目下,而不是bin输出目录下。

                    */

                   TestDll1 INSTANCE = (TestDll1)Native.loadLibrary("TestDll1", TestDll1.class);

                   public void say(WString value);

         }

 

 

如果dll是以stdcall方式输出函数,那么就继承StdCallLibrary。否则就继承默认的Library接口。

接口内部需要一个公共静态常量:instance。

TestDll1 INSTANCE = (TestDll1)Native.loadLibrary("TestDll1", TestDll1.class);

通过这个常量,就可以获得这个接口的实例,从而使用接口的方法。也就是调用外部dll的函数!

 

注意:

1,Native.loadLibrary()函数有2个参数:

    1,dll或者.so文件的名字,但不带后缀名。这符合JNI的规范,因为带了后缀名就不可以跨操作系统平台了。

搜索dll的路径是:

1)项目的根路径

2)操作系统的全局路径、

3)path指定的路径。

 

2,第二个参数是本接口的Class类型。

 

JNA通过这个Class类型,根据指定的dll/.so文件,动态创建接口的实例。

 

2,接口中你只需要定义你需要的函数或者公共变量,不需要的可以不定义。

public void say(WString value);

 

参数和返回值的类型,应该和dll中的C函数的类型一致。

这是JNA,甚至所有跨平台调用的难点。

 

这里,C语言的函数参数是:wchar_t*

JNA中对应的Java类型是WStirng。

 

常见的跨平台调用有:

1,Java调用C语言编写的dll、.so动态链接库中的函数。

2,.NET通过P/Invoke调用C语言编写的dll、.so动态链接库中的函数。

3,通过WEBService,在C,C++,Java,.NET等种种语言间调用。

    WebService传递的是xml格式的数据。

 

即使是强大的P/Invoke或者WebService,在遇到复杂的数据类型和大数据量的传递时,还是会碰到很大的困难。

 

来自 <http://blog.csdn.net/shendl/article/details/3589676>

https://github.com/nativelibs4java/nativelibs4java

 

 

This repository (used to) host / incubated source code for the following (related) projects :

Please read Olivier Chafik's blog for announcements.

 

来自 <https://github.com/nativelibs4java/nativelibs4java>

https://github.com/kaitoy/pcap4j

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Features Automatic mapping from Java to native functions, with simple mappings for all primitive data types Runs on most platforms which support Java Automatic conversion between C and Java strings, with customizable encoding/decoding Structure and Union arguments/return values, by reference and by value Function Pointers, (callbacks from native code to Java) as arguments and/or members of a struct Auto-generated Java proxies for native function pointers By-reference (pointer-to-type) arguments Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer Nested structures and arrays Wide (wchar_t-based) strings Native long support (32- or 64-bit as appropriate) Demo applications/examples Supported on 1.4 or later JVMs, including JavaME (earlier VMs may work with stubbed NIO support) Customizable marshalling/unmarshalling (argument and return value conversions) Customizable mapping from Java method to native function name, and customizable invocation to simulate C preprocessor function macros Support for automatic Windows ASCII/UNICODE function mappings Varargs support Type-safety for native pointers VM crash protection (optional) Optimized direct mapping for high-performance applications. COM support for early and late binding. COM/Typelib java code generator. Community and Support All questions should be posted to the jna-users Google group. Issues can be submitted here on Github. When posting to the mailing list, please include the following: What OS/CPU/architecture you're using (e.g. Windows 7 64-bit) Reference to your native interface definitions (i.e. C headers), if available The JNA mapping you're trying to use VM crash logs, if any Example native usage, and your attempted Java usage It's nearly impossible to indicate proper Java usage when there's no native reference to work from. For commercial support, please contact twalljava [at] java [dot] net. Using the Library Getting Started Functional Description. Mapping between Java and Native Using Pointers and Arrays Using Structures and Unions Using By-Reference Arguments Customization of Type Mapping Callbacks/Function Pointers/Closures Dynamically Typed Languages (JRuby/Jython) Platform Library Direct Method Mapping (Optimization) Frequently Asked Questions (FAQ) Avoiding Crashes Primary Documentation (JavaDoc) The definitive JNA reference is in the JavaDoc. Developers Contributing to JNA Setting up a Windows Development Environment Setting up an Android Development Environment Setting up a RaspberryPi Development Environment Setting up a Mac Development Environment Releasing JNA Publishing to Maven Central Contributing You're encouraged to contribute to JNA. Fork the code from https://github.com/java-native-access/jna and submit pull requests. For more information on setting up a development environment see Contributing to JNA. If you are interested in paid support, feel free to say so on the jna-users mailing list. Most simple questions will be answered on the list, but more complicated work, new features or target platforms can be negotiated with any of the JNA developers (this is how several of JNA's features came into being). You may even encounter other users with the same need and be able to cost share the new development. License This library is licensed under the LGPL, version 2.1 or later, and (from version 4.0 onward) the Apache Software License, version 2.0. Commercial license arrangements are negotiable. NOTE: Oracle is not sponsoring this project, even though the package name (com.sun.jna) might imply otherwise.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值