调用dll函数java [英] calling dll function java

查看:127
本文介绍了调用dll函数java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手,我有第三方dll用于在led显示屏中显示数据。

i试图调用存在于dll中的SendColorTp()函数我的代码是dll.i试图在java中使用system.load()加载dll,但它显示异常java.lang.UnsatisfiedLinkError:led_data_send.SendColorTp(IIIILjava / lang / String; IIII)I

请给出一个从java调用dll的正确方法..

解决方案

我建​​议Java Native Access(JNA)比使用JNI更容易。

请找一个例子(来自维基百科):

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

/ * * Windows本机库声明和用法的简单示例。 * /
public class BeepExampl {
public interface Kernel32扩展了库{
// FREQUENCY以赫兹表示,范围从37到32767
// DURATION以毫秒表示
public boolean Beep( int FREQUENCY, int DURATION);
public void 睡眠( int DURATION);
}
public static void main( String [] args){
Kernel32 lib =(Kernel32)Native.loadLibrary( kernel32
Kernel32。 class );
lib.Beep( 698 500 );
lib.Sleep( 500 );
lib.Beep( 698 500 );
}
}





假设你有一个带有某些功能的DLL,

创建一个java接口,它具有与DLL中的函数相同的方法签名。

例如

  public   interface  NativeExample {

public int method1( String param1);
public boolean mehthod2();

}





以下是加载DLL的方式(假设其名称为NativeLib.dll)



 NativeExample nativeExample =(NativeExample)Native.loadLibrary( < span class =code-string> NativeLib,NativeExample。 class ); 





一旦你有了这个,就可以通过java方法从DLL调用方法。

`nativeExample.method(  test);`
`naturalExample.method2();`


iam new to java ,i have third party dll which are used to display a data in led display.
i trying to call SendColorTp() fuction which are present in dll i dint have a code of dll.i tried to load dll using system.load() in java but it showing an exception java.lang.UnsatisfiedLinkError: led_data_send.SendColorTp(IIIILjava/lang/String;IIII)I
please give an proper way to call dll from java..

解决方案

I would recommend Java Native Access (JNA) as its easier than using JNI.
Please find an example (from Wikipedia):

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

/** Simple example of Windows native library declaration and usage. */
public class BeepExampl{
   public interface Kernel32 extends Library {
       // FREQUENCY is expressed in hertz and ranges from 37 to 32767
       // DURATION is expressed in milliseconds
       public boolean Beep(int FREQUENCY, int DURATION);
       public void Sleep(int DURATION);
   }
   public static void main(String[] args) {
    Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", 
           Kernel32.class);
    lib.Beep(698, 500);
    lib.Sleep(500);
    lib.Beep(698, 500);
   }
}



Lets say you have a DLL with some functions,
Create an java interface which has the same method signatures as the functions in DLL.
For example

public interface NativeExample{

  public int method1(String param1);
  public boolean mehthod2();

}



Now following is the way you load the DLL (assuming its name is NativeLib.dll)

NativeExample nativeExample= (NativeExample)Native.loadLibrary("NativeLib", NativeExample.class);



Once you have this, you can call the method from the DLL via java methods.

`nativeExample.method("test");`
`nativeExample.method2();`


这篇关于调用dll函数java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆