Use JNI instead of JNA to call native code?

后端 未结 10 2079
滥情空心
滥情空心 2020-11-28 18:42

JNA seems a fair bit easier to use to call native code compared to JNI. In what cases would you use JNI over JNA?

10条回答
  •  一整个雨季
    2020-11-28 18:46

    In my specific application, JNI proved far easier to use. I needed to read and write continuous streams to and from a serial port -- and nothing else. Rather than try to learn the very involved infrastructure in JNA, I found it much easier to prototype the native interface in Windows with a special-purpose DLL that exported just six functions:

    1. DllMain (required to interface with Windows)
    2. OnLoad (just does an OutputDebugString so I can know when Java code attaches)
    3. OnUnload (ditto)
    4. Open (opens the port, starts read and write threads)
    5. QueueMessage (queues data for output by the write thread)
    6. GetMessage (waits for and returns data received by the read thread since the last call)

提交回复
热议问题