10 Şubat 2020 Pazartesi

Java Native Access - JNA

Giriş
Açıklaması şöyle.
Java Native Access (JNA) and Java Native Interface (JNI) are two frameworks that allow Java to directly call C-style functions located in executable libraries, such as Windows Dynamic Link Libraries (DLLs). Very often the Java code seeks to directly access functions inside of the WIN32 API.
Windows
Eğer Windows kullanıyorsak JVM'in bazı dll'lere erişimi olmalı. Açıklaması şöyle.
JNA requires some native libraries be properly installed. E.g. it needs jnidispatch.dll for Windows platform.
Yoksa şöyle bir hata alırız
java.lang.UnsatisfiedLinkError: The specified module could not be found.
Maven
Şu satırı dahil ederiz.
<dependency> <!-- JNA dependency -->
  <groupId>net.java.dev.jna</groupId>
  <artifactId>jna</artifactId>
  <version>5.5.0</version>
</dependency>

<dependency> <!-- JNA dependency -->
  <groupId>net.java.dev.jna</groupId>
  <artifactId>jna-platform</artifactId>
  <version>5.5.0</version>
</dependency>
Açıklaması şöyle. Github'daki açıklama şöyle.
JNA is the core stuff. Platform has all the platform-specific stuff like constants and interface mappings for supported OS's (though are not 100% comprehensive, as is expected).

You don't technically need the platform jar, but it will save you a lot of time.
Şu jar'lar gelir.
jna.jar, jna-3.2.5.jar ve jna-3.3.0-platform.jar

StdCallLibrary Arayüzü
Örnek
Şöyle bir API olsun.
BOOL WINAPI MagImageScalingCallback(
  _In_  HWND           hwnd,
  _In_  void           *srcdata,
  _In_  MAGIMAGEHEADER srcheader,
  _Out_ void           *destdata,
  _In_  MAGIMAGEHEADER destheader,
  _In_  RECT           unclipped,
  _In_  RECT           clipped,
  _In_  HRGN           dirty
);
JNA ile çağrımak için şöyle yaparız.
public interface MagImageScalingCallback extends StdCallLibrary.StdCallCallback{
  public boolean MagImageScalingCallback(HWND hwnd,
    Pointer srcdata,
    MAGIMAGEHEADER.ByValue srcheader,
    Pointer destdata,
    MAGIMAGEHEADER.ByValue destheader,
    RectByValue source,
    RectByValue clipped,
    HRGN dirty);
}

Hiç yorum yok:

Yorum Gönder