本文介绍了如何在Java程序中的DLL中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用JNA在DLL中调用一个方法。到目前为止,使用

  Runtime.getRuntime()加载DLL(myworkspace / test.dll); 

这个dll包含一个我需要访问的方法。如何在Java文件中执行DLL中存在的方法。我创建一个对象或DLL的东西,然后在点运算符之后获取方法名称。

解决方案

从:

  package jnahelloworldtest; 

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna。*;

/ **本机库声明和使用的简单示例。 * /
public class Main {
public interface simpleDLL extends Library {
simpleDLL INSTANCE =(simpleDLL)Native.loadLibrary(
(Platform.isWindows()?simpleDLL simpleDLLLinuxPort),simpleDLL.class);
//可以检查运行哪个程序的平台,例如,我们假设库中有一个linux端口(它不附加到可下载项目)
byte giveVoidPtrGetChar(指针参数); // char giveVoidPtrGetChar(void * param);
int giveVoidPtrGetInt(指针参数); // int giveVoidPtrGetInt(void * param);
int giveIntGetInt(int a); // int giveIntGetInt(int a);
void simpleCall(); // void simpleCall();
}

public static void main(String [] args){

simpleDLL sdll = simpleDLL.INSTANCE;

sdll.simpleCall(); //调用void函数

int a = 3;
int result1 = sdll.giveIntGetInt(a); //调用函数int参数& result
System.out.println(giveIntGetInt(+ a +):+ result1);

String testStr =ToBeOrNotToBe;
内存mTest =新内存(testStr.length()+ 1); //'+1'记住\0字符的额外字节!
mTest.setString(0,testStr);
String testReturn = mTest.getString(0); //您可以看到String已正确存储在Memory对象中
System.out.println(Memory in Memory:+ testReturn);

内存intMem =新内存(4); //分配空间
intMem.setInt(0,666); //将分配的内存设置为一个整数
指针intPointer = intMem.getPointer(0);

int int1 = sdll.giveVoidPtrGetInt(Pointer.NULL); //传递null,获取默认结果
System.out.println(giveVoidPtrGetInt(null):+ int1); //传递存储在内存对象中的int,使其返回
int int2 = sdll.giveVoidPtrGetInt(intMem);
// int int2 = sdll.giveVoidPtrGetInt(intPointer);导致JVM崩溃,直接使用内存对象!
System.out.println(giveVoidPtrGetInt(666):+ int2);

字节char1 = sdll.giveVoidPtrGetChar(Pointer.NULL); //传递null,获取默认结果
字节char2 = sdll.giveVoidPtrGetChar(mTest); //传递存储在Memory对象中的字符串,获取第一个字母

System.out.println(giveVoidPtrGetChar(null):+(char)char1);
System.out.println(giveVoidPtrGetChar('ToBeOrNotToBe'):+(char)char2);

}
}


I am trying to call a method in a DLL using JNA. So far have loaded the DLL using

Runtime.getRuntime().load("myworkspace/test.dll");

This dll contaings a method that I need to access. How can I execute the method present in DLL in my Java file. Do I create an object or something of the DLL and then get the method name after the dot operator.

解决方案

From the source:

package jnahelloworldtest;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.*;

/** Simple example of native library declaration and usage. */
public class Main {
    public interface simpleDLL extends Library {
        simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(
            (Platform.isWindows() ? "simpleDLL" : "simpleDLLLinuxPort"), simpleDLL.class);
        // it's possible to check the platform on which program runs, for example purposes we assume that there's a linux port of the library (it's not attached to the downloadable project)
        byte giveVoidPtrGetChar(Pointer param); // char giveVoidPtrGetChar(void* param);
        int giveVoidPtrGetInt(Pointer param);   //int giveVoidPtrGetInt(void* param);
        int giveIntGetInt(int a);               // int giveIntGetInt(int a);
        void simpleCall();                      // void simpleCall();
    }

    public static void main(String[] args) {

        simpleDLL sdll = simpleDLL.INSTANCE;

        sdll.simpleCall();  // call of void function

        int a = 3;
        int result1 = sdll.giveIntGetInt(a);  // calling function with int parameter&result
        System.out.println("giveIntGetInt("+a+"): " + result1);

        String testStr = "ToBeOrNotToBe";
        Memory mTest = new Memory(testStr.length()+1);  // '+1' remember about extra byte for \0 character!
        mTest.setString(0, testStr);
        String testReturn = mTest.getString(0); // you can see that String got properly stored in Memory object
        System.out.println("String in Memory:"+testReturn);

        Memory intMem = new Memory(4);  // allocating space
        intMem.setInt(0, 666); // setting allocated memory to an integer
        Pointer intPointer = intMem.getPointer(0);

        int int1 = sdll.giveVoidPtrGetInt(Pointer.NULL); // passing null, getting default result
        System.out.println("giveVoidPtrGetInt(null):" + int1); // passing int stored in Memory object, getting it back
        int int2 = sdll.giveVoidPtrGetInt(intMem);
       //int int2 = sdll.giveVoidPtrGetInt(intPointer);  causes JVM crash, use memory object directly!
        System.out.println("giveVoidPtrGetInt(666):" + int2);

        byte char1 = sdll.giveVoidPtrGetChar(Pointer.NULL);  // passing null, getting default result
        byte char2 = sdll.giveVoidPtrGetChar(mTest);        // passing string stored in Memory object, getting first letter

        System.out.println("giveVoidPtrGetChar(null):" + (char)char1);
        System.out.println("giveVoidPtrGetChar('ToBeOrNotToBe'):" + (char)char2);

    }
}

这篇关于如何在Java程序中的DLL中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 02:56