I want to learn how to do what JNA does by writing my own JNI code

I am a beginner and I tested the jNA jar library exactly jna-4.1.0.jar

and jna-platform-4.1.0.jar

.

I tested performance using Kernel32, User32, WinBase, WinDef, WinN and WinUser libraries.

But now I don’t want to use JNA (I know it’s better and easier) I want to use JNI (it’s so hard) (I may be going against the flow) !!!

I want to get rid of dependency on external Java jar files

Question : What object can replace a JNA object with something like a pure Java object?

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.Pointer;
import com.sun.jna.Union;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;

      

Question . I have some code to test using JNA. But I need to know how to translate this structure using pure Java?

Example SYSTEM_INFO structure

public static class SYSTEM_INFO extends Structure {
  public static class ByReference extends SYSTEM_INFO
  implements Structure.ByReference { }
  public static class UNION extends Union {
    public static class ByReference extends UNION
    implements Structure.ByReference { }

    public static class IntStruct extends Structure {
      public static class ByValue extends IntStruct 
      implements Structure.ByValue {}
      public short wProcessorArchitecture;
      public short  wReserved;
    }
    public int dwOemId;
    public IntStruct s;
  }
  int dwPageSize;
  Pointer lpMinimumApplicationAddress;
  Pointer lpMaximumApplicationAddress;
  NativeLong  dwActiveProcessorMask;
  int dwNumberOfProcessors;
  int dwProcessorType;
  int dwAllocationGranularity;
  short wProcessorLevel;
  short wProcessorRevision;
}

      

Other structure OSVERSIONINFOEX structure

typedef struct _OSVERSIONINFOEX {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  TCHAR szCSDVersion[128];
  WORD  wServicePackMajor;
  WORD  wServicePackMinor;
  WORD  wSuiteMask;
  BYTE  wProductType;
  BYTE  wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;

      

Perhaps this is the easy part:

  public interface Advapi32 extends Library {
    Advapi32 INSTANCE = (Advapi32)Native.loadLibrary("advapi32", Advapi32.class);
    boolean GetUserNameA(byte[] name, IntByReference intRef);
  }

      

PD I need a real tutorial on using JNI DLL functions (not Hello World), using pointers and data structures, etc. etc.

Sorry if I lose ... Thanks!

+3


source to share


1 answer


1) Take all this code that uses a Library

JNA based interface and rewrite it in C

, compile it into a DLL.

2) Create an API in JNI so Java can talk to your new code C

(presumably somewhat smaller than the full win32 API you are using)

3) Write code to translate native structures into Java objects or primitives.

There is no magic translation from native structures to Java objects, although you might consider SWIG, which can generate code for you. There's also an example here .



EDIT

If you want to really learn JNI, start with some simple functions and move on to more advanced ones, one at a time. JNA unit tests can provide you with some information about the range of data that you can pass between Java and native. Its own library jnidispatch

,, covers almost every interaction that can be generic.

  • function return functions
  • function arguments
  • complex data display ( struct

    / Structure

    )
  • function pointers ( Callback

    ) - native return code in Java

Of course, JNA's fair bit has to do with dynamically and automatically calculating how to translate Java bits into native bits, which you can avoid when you manually write JNI code as you can be explicit by whatever C

code you write.

0


source







All Articles