- 浏览: 985662 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (826)
- 硬件 (8)
- 软件 (24)
- 软件工程 (34)
- JAVA (229)
- C/C++/C# (77)
- JavaScript (8)
- PHP (1)
- Ruby (3)
- MySQL (14)
- 数据库 (19)
- 心情记事 (12)
- 团队管理 (19)
- Hadoop (1)
- spring (22)
- mybatis(ibatis) (7)
- tomcat (16)
- velocity (0)
- 系统架构 (6)
- JMX (8)
- proxool (1)
- 开发工具 (16)
- python (10)
- JVM (27)
- servlet (5)
- JMS (26)
- ant (2)
- 设计模式 (5)
- 智力题 (2)
- 面试题收集 (1)
- 孙子兵法 (16)
- 测试 (1)
- 数据结构 (7)
- 算法 (22)
- Android (11)
- 汽车驾驶 (1)
- lucene (1)
- memcache (12)
- 技术架构 (7)
- OTP-Erlang (7)
- memcached (17)
- redis (20)
- 浏览器插件 (3)
- sqlite (3)
- Heritrix (9)
- Java线程 (1)
- scala (0)
- Mina (6)
- 汇编 (2)
- Netty (15)
- libevent (0)
- CentOS (12)
- mongod (5)
- mac os (0)
最新评论
-
kingasdfg:
你这里面存在一个错误添加多个任务 应该是这样的 /** * ...
Quartz的任务的临时启动和暂停和恢复【转】 -
kyzeng:
纠正一个错误,long型对应的符号是J,不是L。
Jni中C++和Java的参数传递 -
zhaohaolin:
抱歉,兄弟,只是留下作记录,方便学习,如果觉得资料不好,可以到 ...
netty的个人使用心得【转】 -
cccoooccooco:
谢谢!自己一直以为虚机得使用网线才可以与主机连接呢。。
主机网卡无网线连接与虚拟机通信 -
yuqilin001:
要转别人的东西,请转清楚点嘛,少了这么多类,误人子弟
netty的个人使用心得【转】
Java Native Access (JNA) has a single component, Following is an example of mapping for the Windows kernel32 library. See the JavaDoc overview for more detailed information about JNA usage. NOTE: if the parameter is to be used by the native function outside the scope of the function call, you must use Arrays of C strings (the Typically you define a If a function requires an array of Unions are generally interchangeable with The Type-safe pointers may be declared by deriving from the Alternatively, user-defined types may implement the NativeMapped interface, which determines conversion to and from native types on a class-by-class basis. You may also customize the mapping of Java method names to the corresponding native function name. TheStdCallFunctionMapper is one implementation which automatically generates Refer to this table in the overview for a complete list of built-in type mappings. Proxy wrappers are automatically generated for function pointers found within structs initialized by native code. This facilitates calling those functions from Java. Here's a brief example of using JNA from JRuby: JNA includes Before you map your own functions, check the platform package documentation for an already mapped one. Platform-specific structures are mapped by header. For example, Cross-platform functions and structures are implemented in How To Get Started Using JNA
jna.jar
; the supporting native library (jnidispatch
) is included in the jar file. JNA is capable of extracting and loading the native library on its own, so you don't need additional configuration. JNA falls back to extraction if the native library is not already installed on the local system somewhere accessible to System.loadLibrary
(see information on library loading). The native library is also available in platform-specific jar files for use with Java Web Start.
If the C header files for your library are available, you can auto-generate a library mapping by using Olivier Chafik's excellent JNAerator utility. This is especially useful if your library uses long or complicated structures where translating by hand can be error-prone.
jna.jar
from the download page.printf
function from the standard C library and calls it. Be sure to include jna.jar in the classpath:
package com.sun.jna.examples; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; public class HelloWorld { // 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.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); 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]); } } }
jna.library.path
system property to the path to your target library. This property is similar to java.library.path
but only applies to libraries loaded by JNA.Library
interface.
package com.sun.jna.examples.win32; import com.sun.jna.*; // 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 { // Method declarations, constant and structure definitions go here }
Native.loadLibrary(Class)
method, providing the native library interface you defined in step (5).
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("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(INSTANCE);
The INSTANCE variable is for convenient reuse of a single instance of the library. Alternatively, you can load the library into a local variable so that it will be available for garbage collection when it goes out of scope. AMap
of options may be provided as the third argument to loadLibrary
to customize the library behavior; some of these options are explained in more detail below. The SYNC_INSTANCE is also optional; use it if you need to ensure that your native library has only one call to it at a time.Structure
and add public fields (which may include arrays or nested structures).
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);
Kernel32 lib = Kernel32.INSTANCE; SYSTEMTIME time = new SYSTEMTIME(); lib.GetSystemTime(time); System.out.println("Today's integer value is " + time.wDay);
Native.register(String)
in the class static initializer with your library's name. See JNA direct mapping for an example.
Default Type Mappings
Java primitive types (and their object equivalents) map directly to the native C type of the same size.
Unsigned types use the same mappings as signed types. C enums are usually interchangeable with "int". A more comprehensive list of mappings may be found here.
Native Type
Size
Java Type
Common Windows Types
char
8-bit integer
byte
BYTE, TCHAR
short
16-bit integer
short
WORD
wchar_t
16/32-bit character
char
TCHAR
int
32-bit integer
int
DWORD
int
boolean value
boolean
BOOL
long
32/64-bit integer
NativeLong
LONG
long long
64-bit integer
long
__int64
float
32-bit FP
float
double
64-bit FP
double
char*
C string
String
LPTCSTR
void*
pointer
Pointer
LPVOID, HANDLE, LPXXX
Using Pointers and Arrays
Primitive array arguments (including struct
s) are represented by their corresponding Java types. For example:
// Original C declarations void fill_buffer(int *buf, int len); void fill_buffer(int buf[], int len); // same thing with array syntax // Equivalent JNA mapping void fill_buffer(int[] buf, int len);
Memory
or an NIO Buffer. The memory provided by a Java primitive array will only be valid for use by the native code for the duration of the function call.char* argv[]
to the C main
, for example), may be represented by String[]
in Java code. JNA will pass an equivalent array with a NULL final element.Using Structures/Unions
When a function requires a pointer to a struct
, a Java Structure should be used. If the struct
is passed or returned by value, you need only make minor modifications to the parameter or return type class declaration.
public static
class derived from Structure
within your library interface definition. This allows the structure to share any options (like custom type mapping) defined for the library interface.struct
(allocated contiguously in memory), a Java Structure[]
may be used. When passing in an array of Structure
, it is not necessary to initialize the array elements (the function call will allocate, zero memory, and assign the elements for you). If you do need to initialize the array, you should use theStructure.toArray
method to obtain an array of Structure
elements contiguous in memory, which you can then initialize as needed.Structure
s, but require that you indicate which union field is active with the setType
method before it can be properly passed to a function call.Using By-reference Arguments
When a function accepts a pointer-to-type argument you can use one of the ByReference
types to capture the returned value, or subclass your own. For example:
Alternatively, you could use a Java array with a single element of the desired type, but the
// Original C declaration void allocate_buffer(char **bufp, int* lenp); // Equivalent JNA mapping void allocate_buffer(PointerByReference bufp, IntByReference lenp); // Usage PointerByReference pref = new PointerByReference(); IntByReference iref = new IntByReference(); lib.allocate_buffer(pref, iref); Pointer p = pref.getValue(); byte[] buffer = p.getByteArray(0, iref.getValue());
ByReference
convention better conveys the intent of the code.
Pointer
class provides a number of accessor methods in addition to getByteArray()
which effectively function as a typecast onto the memory.PointerType
class.Customized Mapping from Java to Native (Types and Function Names)
The TypeMapper class and related interfaces provide for converting any Java type used as an argument, return value, or structure member to be converted to or from a native type. The example w32 API interfaces use a type mapper to convert Java boolean into the w32 BOOL
type. A TypeMapper
instance is passed as the value for the TYPE_MAPPER key in the options map passed to Native.loadLibrary
.
stdcall
-decorated function names from a Java interface method signature. The mapper should be passed as the value for theOPTION_FUNCTION_MAPPER key in the options map passed to the Native.loadLibrary
call.Callbacks/Closures (Function Pointers)
Callback declarations consist of a simple interface that extends the Callback interface and implements a callback
method (or defines a single method of arbitrary name). Callbacks are implemented by wrapping a Java object method in a little bit of C glue code. The simplest usage resembles using anonymous inner classes to register event listeners. Following is an example of callback usage:
Here is a more involved example, using the w32 APIs to enumerate all native windows:
// Original C declarations typedef void (*sig_t)(int); sig_t signal(sig_t); // Equivalent JNA mappings public interface CLibrary extends Library { int SIGUSR1 = 30; interface sig_t extends Callback { void invoke(int signal); } sig_t signal(int sig, sig_t fn); int raise(int sig); } ... CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class); // WARNING: you must keep a reference to the callback object // until you deregister the callback; if the callback object // is garbage-collected, the native callback invocation will // probably crash. CLibrary.sig_t fn = new CLibrary.sig_t() { public void invoke(int sig) { System.out.println("signal " + sig + " was raised"); } }; CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn); lib.raise(CLibrary.SIGUSR1); ...
If your callback needs to live beyond the method invocation where it is used, make sure you keep a reference to it or the native code will call back to an empty stub after the callback object is garbage collected.
// Original C declarations typedef int (__stdcall *WNDENUMPROC)(void*,void*); int __stdcall EnumWindows(WNDENUMPROC,void*); // Equivalent JNA mappings public interface User32 extends StdCallLibrary { interface WNDENUMPROC extends StdCallCallback { boolean callback(Pointer hWnd, Pointer arg); } boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg); } ... User32 user32 = User32.INSTANCE; user32.EnumWindows(new WNDENUMPROC() { int count; public boolean callback(Pointer hWnd, Pointer userData) { System.out.println("Found window " + hWnd + ", total " + ++count); return true; } }, null);
Invocation from Dynamically-Typed Languages
Languages such as Jython or JRuby may find it more convenient to access the NativeLibrary and Function classes directly rather than establishing a dedicated interface.
require 'java' module Libc @@lib = com.sun.jna.NativeLibrary.getInstance("c") @@ptr_funcs = [ 'fopen', 'malloc', 'calloc' ] def self.method_missing(meth, *args) if @@ptr_funcs.include?(meth.to_s) @@lib.getFunction(meth.to_s).invokePointer(args.to_java) else @@lib.getFunction(meth.to_s).invokeInt(args.to_java) end end O_RDONLY = 0 O_WRONLY = 1 O_RDWR = 2 end Libc.puts("puts from libc") Libc.printf("Hello %s, from printf\n", "World") file = Libc.open("/dev/stdout", 1, Libc::O_WRONLY) n = Libc.write(file, "Test\n", 5) puts "Wrote #{n} bytes via Libc" path = "/dev/stdout" fp = Libc.fopen(path, "w+") Libc.fprintf(fp, "fprintf to %s via stdio\n", path) Libc.fflush(fp) Libc.fclose(fp)
Platform Library
platform.jar
that has cross-platform mappings and mappings for a number of commonly used platform functions, including a large number of Win32 mappings as well as a set of utility classes that simplify native access. The code is tested and the utility interfaces ensure that native memory management is taken care of correctly.ShlObj.h
structures can be found incom.sun.jna.platform.win32.ShlObj
. Platform functions are mapped by library. For example, Advapi32.dll
functions can be found in com.sun.jna.platform.win32.Advapi32
. Simplified interfaces (wrappers) forAdvapi32.dll
functions can be found in com.sun.jna.platform.win32.Advapi32Util
.com.sun.jna.platform
. These currently include the following.
FileMonitor
: a cross-platform file system watcherFileUtils
: a cross-platform set of file-related functions, such as move to the recycle binKeyboardUtils
: a cross-platform set of keyboard functions, such as finding out whether a key is pressedWindowUtils
: a cross-platform set of window functions, providing non-rectangular shaped and transparent windows
发表评论
-
调试jdk中的源码,查看jdk局部变量
2013-06-15 23:30 1013调试jdk中的源码,查看jdk局部变量 2012-04 ... -
Eclipse快捷键 10个最有用的快捷键<转>
2013-04-11 23:28 1011Eclipse中10个最有用的快捷键组合 一个Eclip ... -
Lucene 3.6 中文分词、分页查询、高亮显示等
2012-12-09 23:35 17731、准备工作 下载lucene 3.6.1 : htt ... -
Maven实战(九)——打包的技巧(转)
2012-10-12 00:41 899“打包“这个词听起 ... -
基于Maven的web工程如何配置嵌入式Jetty Server开发调试环境(转)
2012-10-12 00:28 8721、首先在web工程的POM文件里添加依赖jar包如下: ... -
轻轻松松学Solr(1)--概述及安装[转]
2012-09-18 14:59 950概述 这段时间对企 ... -
分析Netty工作流程[转]
2012-09-04 19:02 846下面以Netty中Echo的例 ... -
让eclipse在ubuntu下面好看一点
2012-03-27 10:17 883<p> </p> <h1 cla ... -
zookeeper安装和应用场合(名字,配置,锁,队列,集群管理)[转]
2012-01-12 17:59 1615安装和配置详解 本文 ... -
Jakarta-Common-BeanUtils使用笔记[转]
2012-01-10 14:13 1120Jakarta-Common-BeanUtils ... -
一个关于Java Thread wait(),notify()的实用例【转】
2012-01-07 16:05 984///// // ProducerConsume ... -
Java基础:Java中的 assert 关键字解析【转】
2012-01-06 19:50 976J2SE 1.4在语言上提供了 ... -
一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发[转]
2012-01-06 15:02 1217六种异常处理的陋习 ... -
如何解决HP QC(Quality Center)在Windows 7下不能工作的问题
2011-12-26 10:48 1538HP QC(Quantity Center) 是一款不错的测 ... -
JAVA读写文件,中文乱码 【转】
2011-12-19 23:43 2073最近在做HTML静态生成,需要从硬盘上把模版文件的内容读出来。 ... -
Java 6 JVM参数选项大全(中文版)【转】
2011-12-19 19:51 927Java 6 JVM参数选项大全(中文版) 作者 ... -
使用assembly plugin实现自定义打包【转】
2011-12-13 01:58 925在上一篇文章中,讨论到在对maven的机制不熟悉的情况下,为了 ... -
使用maven ant task实现非标准打包[转]
2011-12-13 01:56 1005maven很强大,但是总有些事情干起来不是得心应手,没有使用a ... -
Java日期转换SimpleDateFormat格式大全【转】
2011-12-08 20:22 126524小时制时间 显示: public clas ... -
使用Spring的表单标签库
2011-11-22 20:08 103413.9. 使用Spring的 ...
相关推荐
JNA提供一组Java工具类用于在运行期动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java...
jna-4.0.0.jar (用于Java调用C++ DLL动态库)
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
JNA-5.7.0 jna-platform-5.7.0
赠送jar包:jna-platform-5.10.0.jar; 赠送原API文档:jna-platform-5.10.0-javadoc.jar; 赠送源代码:jna-platform-5.10.0-sources.jar; 赠送Maven依赖信息文件:jna-platform-5.10.0.pom; 包含翻译后的API文档...
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
赠送jar包:jna-4.1.0.jar; 赠送原API文档:jna-4.1.0-javadoc.jar; 赠送源代码:jna-4.1.0-sources.jar; 赠送Maven依赖信息文件:jna-4.1.0.pom; 包含翻译后的API文档:jna-4.1.0-javadoc-API文档-中文(简体)版...
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
学习java调用c++时用到的JNA包,分享出来和大家一起学习
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
在开发项目过程中,需要处理算法分析出来二进制流数据,并对数据解析处理转发。我们上层应用平台是使用java开发的,但开放平台是使用c++开发,虽提供给我们对应的.h头文件,但在java中来解析这些数据就显然有些复杂...
自动更根据Java文件生成JNI接口(C++虚类)的小工具, 该工具是运行在UNIX/Linux之上的,内有源码。 工具的实现用到了shell、c语言以及c和java工具链。 压缩包里面有README.TXT可以看说明,有一个范例JniCore.java....
(2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <groupId>net.java.dev.jna</groupId> <artifactId>jna *** ``` # Gradle依赖: ``...
java使用(jna)调用c/c++第三方动态库 dll文件 所用jar包
JNA(Java Native Access )提供一组Java工具类用于在运行期动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与...
赠送jar包:jna-platform-4.1.0.jar; 赠送原API文档:jna-platform-4.1.0-javadoc.jar; 赠送源代码:jna-platform-4.1.0-sources.jar; 赠送Maven依赖信息文件:jna-platform-4.1.0.pom; 包含翻译后的API文档:...
赠送jar包:jna-platform-4.3.0.jar; 赠送原API文档:jna-platform-4.3.0-javadoc.jar; 赠送源代码:jna-platform-4.3.0-sources.jar; 赠送Maven依赖信息文件:jna-platform-4.3.0.pom; 包含翻译后的API文档:...
基于spring-boot2.3.0,net.java.dev.jna5.9.0开发的JNI和JNA的web工程。 主要包括:结构体传参,so加载,so文件调用等
Java调用c++ dll文件所需jar包,适用于java 对接 c++,瞧一瞧看一看了啊,现在积分太珍贵,必须要养家糊口了,赚点积分方便后面用