`
lhy5201314
  • 浏览: 121322 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

JNative

阅读更多
Dealing with out structures
Friday 26 January 2007.

By the same authors
jnative
JNative Howto
Adding a simple function call to User32/Kernel32/Foo java wrapper class
Callbacks are working now !!!!
New features on JNative v1.0
Download/Téléchargement
This is a simple DLL call that puts its output in a structure :

I take the example of Kernel32’s GetSystemTime() for example as asked by Rods

This is the comment of the out structure you can find it on the msdn


        /**
         * SystemTime
         *
         * <pre>
         *         typedef struct _SYSTEMTIME {
         *                   WORD wYear;
         *                   WORD wMonth;
         *                   WORD wDayOfWeek;
         *                   WORD wDay;
         *                   WORD wHour;
         *                   WORD wMinute;
         *                   WORD wSecond;
         *                   WORD wMilliseconds;
         *                 } SYSTEMTIME,
         * </pre>
         */

Then we need to translate that in a Java class. It will be based on a help class that was designed for that usage : AbstractBasicData this class is a generic one since its method getValue() will return an object of the parametrized type. So this its declaration :


        public static class SystemTime extends AbstractBasicData<SystemTime> {


Note the static keyword since GetSystemTime() will be a static method of Kernel32 and SystemTime an inner class of Kernel32.

The C WORD type is two byte storage area : its counterpart in Java is short type so :


                public short wYear;
                public short wMonth;
                public short wDayOfWeek;
                public short wDay;
                public short wHour;
                public short wMinute;
                public short wSecond;
                public short wMilliseconds;

Note the public access : this is not very good design since those members may only be read by Java classes. They should be private with only a getter method. This article is not a Java course so we don’t mind.

We need to implement abstract methods of AbstractBasicData :

  public int getSizeOf()

This method must return the size[color=red][/color] of the structure. Here is 16 since a WORD is 2 bytes and there is 8 WORDS.


                public int getSizeOf() {
                        return 8*2; //8 WORDS of 2 bytes
                }

  public Pointer createPointer()

This method creates a new native storage area, it’s generally called once in the constructor.


                public Pointer createPointer() throws NativeException {
                        pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
                        return pointer;
                }

The pointer member is inherited from AbstractBasicData.

  public SystemTime getValueFromPointer()

This method copies the native values in the Java variables. Do not misuse this method and getValue() getValue() returns the Java object without coping the native values !!! If you don’t mind loosing some milliseconds, override getValue() and return getValueFromPointer() in it.


                public SystemTime getValueFromPointer() throws NativeException {
                        wYear          = getNextShort();
                        wMonth         = getNextShort();
                        wDayOfWeek     = getNextShort();
                        wDay           = getNextShort();
                        wHour          = getNextShort();
                        wMinute        = getNextShort();
                        wSecond        = getNextShort();
                        wMilliseconds  = getNextShort();
                        return this;
                }

  The constructor

The AbstractBasicData constructor takes one parameter which is the default value, in this case we only need to create the native memory block.


                public SystemTime() throws NativeException {
                        super(null);
                        createPointer();
                        mValue = this;
                }


Note the mValue = this; line, so when calling getValue() it will return this.

  The function call in the Kernel32 DLL :


        public static SystemTime GetSystemTime() throws Exception {
                //Create a JNative object called nGetSystemTime : here
                JNative nGetSystemTime = new JNative("Kernel32.dll", "GetSystemTime");
                //Create a SystemTime object that holds the native out structure
                SystemTime systemTime = new SystemTime();
                //Pass its pointer address as the first parameter (first is zero !!!)
                nGetSystemTime.setParameter(0, systemTime.getPointer());
                //Invoke the native GetSystemTime function
                nGetSystemTime.invoke();
                //Return the populated SystemTime object
                return systemTime.getValueFromPointer();
        }

And now the complete code :


--------------------------------------------------------------------------------


        /**
         * SystemTime
         *
         * <pre>
         *         typedef struct _SYSTEMTIME {
         *                   WORD wYear;
         *                   WORD wMonth;
         *                   WORD wDayOfWeek;
         *                   WORD wDay;
         *                   WORD wHour;
         *                   WORD wMinute;
         *                   WORD wSecond;
         *                   WORD wMilliseconds;
         *                 } SYSTEMTIME,
         * </pre>
         */
        public static class SystemTime extends AbstractBasicData<SystemTime> {
                public short wYear;
                public short wMonth;
                public short wDayOfWeek;
                public short wDay;
                public short wHour;
                public short wMinute;
                public short wSecond;
                public short wMilliseconds;
               
                public Pointer createPointer() throws NativeException {
                        pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
                        return pointer;
                }
                public int getSizeOf() {
                        return 8*2; //8 WORDS of 2 bytes
                }
                public SystemTime getValueFromPointer() throws NativeException {
                        wYear          = getNextShort();
                        wMonth         = getNextShort();
                        wDayOfWeek     = getNextShort();
                        wDay           = getNextShort();
                        wHour          = getNextShort();
                        wMinute        = getNextShort();
                        wSecond        = getNextShort();
                        wMilliseconds  = getNextShort();
                        return this;
                }

                public SystemTime() throws NativeException {
                        super(null);
                        createPointer();
                        mValue = this;
                }
               
                @Override
                public String toString() {
                        return wYear + "/" + wMonth+"/"+wDay+" at + "+wHour+":"+wMinute+":"+wSecond+":"+wMlliseconds;
                }
        }

        public static SystemTime GetSystemTime() throws NativeException, IllegalAccessException {
                JNative nGetSystemTime = new JNative(DLL_NAME, "GetSystemTime");
                SystemTime systemTime = new SystemTime();
                nGetSystemTime.setParameter(0, systemTime.getPointer());
                nGetSystemTime.invoke();
                return systemTime.getValueFromPointer();
        }

        public static void main(String[] args) throws NativeException, IllegalAccessException {
                System.err.println(GetSystemTime());
        }


--------------------------------------------------------------------------------

And the output : 2007/1/26 at 21:45:19:718

I hope this little tutorial can help some of you lazy guys ;)

— Marc

Reply to this article
分享到:
评论

相关推荐

    JNative v1.3 docs

    jnative包用于调用dll动态库,目前(2011-12-20)最新版本为1.4RC2,官方正式版本文档为1.3,详见官方地址: http://jnative.free.fr/docs/ 这是最新的帮助文档HTML离线包,希望对大家有帮助 Packages org.xvolks....

    Jnative-1.3.2及心得

    由于项目要求,需要用Java调用windows的dll文件,查了一下,如果用JNI的话是比较麻烦的,在sourceforge.net上搜索了一下“Java dll”,首先出现的是Jnative,于是决定用它,后来也试了些别的,但还是JNative好使,...

    JNative_1.4RC3_src

    由于项目要求,需要用Java调用windows的dll文件,查了一下,如果用JNI的话是比较麻烦的,在sourceforge.net上搜索了一下“Java dll”,首先出现的是Jnative,于是决定用它,后来也试了些别的,但还是JNative好使,...

    jnative简单的使用

    jnative jnative jnative jnative

    jnative使用方法总结

    jnative使用方法总结jnative使用方法总结jnative使用方法总结

    JNative.jar以及JNative源码

    java调用dll时所需的JNative.jar 以及JNativeCpp.dll(win),libJNativeCpp.so(linux) 和JNative的源码

    linux jnative 调用.so方法及附属全部库和源码

    JNative getstring = new JNative("libmylib.so", "getstring"); getstring.setRetVal(Type.STRING); getstring.invoke(); System.out.println(getstring.getRetVal()); } } 4.输出结果 this is in test_a.....

    JNative_1.4RC2&1.4RC3&Jar;整合资源包

    JNative源代码的压缩包把它解压后从中找到libJNativeCpp.so和JNativeCpp.dll两个文件.JNativeCpp.dll应用在Windows平台下.把它放在c:\windows\system32目录下.libJNativeCpp.so应用在Linux平台下 JavaDOC: ...

    Jnative1.4_java调用动态库所需jar

    Jnative1.4_java调用动态库所需jar,里面包含了一个可以测试的动态库,可以自己拿下来玩玩。

    Jnative资源包源码

    Jnative资源包源码Jnative资源包源码Jnative资源包源码Jnative资源包源码Jnative资源包源码Jnative资源包源码

    JNative.jar和JNativeCpp.dll

    包括了JNative.jar包和JNativeCpp.dll,还有一个libJNativeCpp.so文件

    JNative完全自学手册

    手把手帮助你完全深度理解掌握Jnative 核心技术,实例众多

    JNative使用示例

    TestJNative_Eclipse.rar 为MyEclipse工程 JNativeTest_VC.rar为VC ...主要写了一些JNative调用DLL的例子,包含了JNative回调函数的用法与指针的用法,以供参考,希望对使用JNative的开发人员有帮助 有问题大家一起讨论

    JNA.jar-JNative.jar-jinvoke.jar-dll创建-JAVA调用-VC调用

    包含jna.jar,JNative.jar,jinvoke.jar,VC++创建DLL例子,VC++调用DLL例子,JAVA、JNA、JNative、jinvoke调用DLL例子

    JNative Java调用动态链接库jar包

    JNative是目前比较简单的一个Java调用动态链接库jar包,使用简单,容易掌握,大家共同学习。

    JNative api说明文档

    JNative api说明文档,也没什么好说的,自己看吧

    jnative 1.4

    jnative 1.4 jnative 1.4 jnative 1.4 jnative 1.4 jnative 1.4 jnative 1.4 jnative 1.4 jnative 1.4

    JNative相关jar包和dll ,so文件.zip

    jNative.jar jNativeCpp.dll libjNativeCpp.so 免费下载jNative.jar jNativeCpp.dll libjNativeCpp.so 免费下载jNative.jar jNativeCpp.dll libjNativeCpp.so 免费下载jNative.jar jNativeCpp.dll libjNativeCpp.so ...

    JAVA调用动态链接库DLL之JNative学习源码

    JAVA调用动态链接库DLL之JNative学习源码,详细请参考:http://blog.csdn.net/testcs_dn/article/details/26980027

    JNative-1.3.2.zip

    JNative.jar , JNativeCpp.dll ,libJNativeCpp.so,java通过JNative调用动态dll所需的文件jar

Global site tag (gtag.js) - Google Analytics