0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaでDLLを呼び出す方法

Posted at

はじめに

Javaでアプリを作成するにあたり、WindowsのDLLを利用する方法について調べてました。

JNAという仕組みを利用する

C/C++で書かれた共有ライブラリ(.dllなど)をJavaから呼ぶ方法の一つ

準備

以下のjarをビルドパスに追加する
https://mvnrepository.com/artifact/net.java.dev.jna/jna
※呼び出すDLLに必要に応じて追加する必要がある

実装例

メッセージボックスを表示する場合
※user32.dllのMessageboxAを呼び出す場合

ソースコード

    public interface User32 extends Library {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
        int MessageBoxA(Pointer hWnd, String lpText, String lpCaption, int uType);
    }

    public static void main(String[] args) {
        User32.INSTANCE.MessageBoxA(null, "test", "title", 0);
    }

以下のライブラリをimport

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

01.png

実行結果

01.png

このように呼び出しが行えるので他のDLLも同様に呼び出しが可能となる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?