使用Java中的C接口调用DLL [英] Calling DLL with a C interface from Java

查看:161
本文介绍了使用Java中的C接口调用DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在第三方DLL中调用本机方法,该方法具有C接口,方法如下:

  DWORD ExampleInterfaceMethod(DWORD Mode,LPSTR Header); 

我已经使用以下方式成功加载了DLL:

 的System.loadLibrary( DLLNAME); 

我创建了一个方法:

  protected native int ExampleInterfaceMethod(int type,int Nth,byte [] name); 

此方法似乎没有使用正确的变量类型,因为每当我将其称为以下抛出错误:

 java.lang.UnsatisfiedLinkError:com.DLLTest.ExampleInterfaceMethod(II [B] I 



什么变量类型需要在Java中使用才能调用这个方法,或者我还缺少其他东西?

解决方案

使用JNI您需要特殊命名的C函数来实现Java native 方法。您不能简单地添加一个 native 方法来调用现有的C函数 - 而是正常的方法是创建一个调用现有C函数的包装器C函数,并被命名正确的方式。



您可能需要查看 JNA 。这是JNI上的一个包装器,它允许您从Java端调用C函数,而无需手动编写适应的本机代码。


I'm attempting to call native methods within a 3rd party DLL, which has a C interface with methods such as:

DWORD ExampleInterfaceMethod( DWORD Mode, LPSTR Header );

I've successfully loaded the DLL using:

System.loadLibrary("DLLName");

and I've created a method:

protected native int ExampleInterfaceMethod(int type, int Nth, byte[] name);

This method doesn't seem to be using the correct variable types, as whenever I call it the following error is thrown:

java.lang.UnsatisfiedLinkError: com.DLLTest.ExampleInterfaceMethod(II[B)I

What variable types do I need to use in Java in order to call this method, or am I missing something else?

解决方案

With JNI, you need specially-named C functions to implement your Java native methods. You can't simply add a native method to call an existing C function - instead the normal way is creating a "wrapper" C function which calls the existing one, and is named the right way.

You might want to have a look at JNA. This is a wrapper around JNI which allows you to call C functions from the Java side without manually writing adapting native code for this.

这篇关于使用Java中的C接口调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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