简体   繁体   English

如何使用JNA在Java代码中使用dll文件?

[英]How to use a dll file in java code using JNA?

I have to use a dll file (of c code) in my java code, for which I searched allot and found that JNA is the most suitable way for it. 我必须在我的Java代码中使用一个(C代码的)dll文件,我对其分配进行了搜索,发现JNA是最适合的方式。 Therefore, I am trying to write a HelloWorld program using jna-4.1.0.jar. 因此,我试图使用jna-4.1.0.jar编写一个HelloWorld程序。 Below is the c code: 下面是c代码:

//C hello world example
#include <stdio.h>

void __declspec(dllexport) _stdcall helloFromC()
{
  printf("Hello world from the c code! \n");
}

and I make dll for this c code by using the following commands on cmd: 我通过在cmd上使用以下命令来为此C代码制作dll:

gcc -c ctest.c
gcc -shared -0 test.dll ctest.o

running these commands gives me a dll for the c code, which I place at the root of my simple java project. 运行这些命令为我提供了C代码的dll,并将其放置在简单Java项目的根目录下。

Now here is my java class: 现在这是我的java类:

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

public class HelloWorld {

public interface CTest extends Library {
    void helloFromC();
}

 public static void main(String[] args) {
        CTest INSTANCE = (CTest) Native.loadLibrary("ctest", CTest.class);

        INSTANCE.helloFromC();
    }
}

Now when I run this java program on eclipse, I am getting this error: 现在,当我在Eclipse上运行此Java程序时,出现此错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.

Kindly help me to solve this issue as it has already taken much of time! 请帮我解决这个问题,因为它已经花费了很多时间!

gcc -c ctest.c
gcc -shared -o test.dll ctest.o
try     'o'  ^  not '0'

It's finding your dll but it doesn't like it. 它正在找到您的dll,但它不喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM