tags:

views:

165

answers:

1

I am new to JNI. And have a few questions : Can JNI handle every type dll exists in windows? I wanted to link a library but it gives me error. Is it possible JNI and the dll are not compatible?

Excerpt from VB .NET (It 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

Full Code From 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 to me where I might do wrong?

EDIT:

Turn out I should be doing 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: 

JNI access uses mangled names; you will need to wrap the system API in a JNI stub with the correct name and arguments. You can't call a system API directly.

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

For example, given Java:

package com.mycompany.jni;

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

the native method linked is something like:

JNIEXPORT jboolean JNICALL Java_com_mycompany_jni_MyClass_someMethod(JNIEnv *jep, jobject thsObj, jstring arg)               
Software Monkey
You just saved whole day (for researching the wrong direction)
henry
Glad I could help. I still remember what it was like skinning my knees on JNI all those years ago.
Software Monkey