Can JNI handle any DLL files (Windows)?

I am new to JNI. And you have a few questions: Can JNI handle every DLL type on Windows? I wanted to link a library, but that gives me an error. Is it possible that JNI and dll are incompatible?

Excerpt from VB.NET (works)

Private Declare Function ConnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function DisconnectReader Lib "rfidhid.dll" () As Integer
Private Declare Function SetAntenna Lib "rfidhid.dll" (ByVal mode As Integer) As Integer

      

Complete code with Java

public class MainForm {

/**
 * @param args
 */
public native int ConnectReader();
public static void main(String[] args) {
    // TODO Auto-generated method stub

    MainForm mf = new MainForm();

    System.out.println(mf.ConnectReader());
}

static {
  System.loadLibrary("rfidhid");
}

      

}

Error code shown

Exception in thread "main" java.lang.UnsatisfiedLinkError: MainForm.ConnectReader()I
at MainForm.ConnectReader(Native Method)
at MainForm.main(MainForm.java:13)

      

Can anyone point me to where I might be going wrong?

EDIT:

It turns out I have to do this (and it works)

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

public class HelloWorld {
   public interface MyLibrary extends Library {

        public int ConnectReader();     
    }

    public static void main(String[] args) {
     Kernel32 lib = (MyLibrary) Native.loadLibrary("rfidhid", MyLibrary.class);
     System.out.println(lib.ConnectReader());
    }
 }

      

+2


a source to share


1 answer


JNI access uses malformed names; you will need to wrap the system API in a JNI stub with the correct name and arguments. You cannot directly access the system API.

I recommend researching JNI thoroughly, or using something like JNA to ease some drudge work.

For example, given Java:



package com.mycompany.jni;

public MyClass
{
native boolean someMethod(String arg)
...
}

      

its associated native method is like:

JNIEXPORT jboolean JNICALL Java_com_mycompany_jni_MyClass_someMethod(JNIEnv *jep, jobject thsObj, jstring arg)               

      

+3


a source







All Articles