如何远程加载DLL库?

时间:2015-02-18 22:43:38

标签: java java-native-interface jna

我有一台安装了应用程序的远程计算机,并且用C编写的API编译成dll。

我希望使用通过远程通过JNA加载dll公开的API与应用程序进行交互。即,我的客户端代码需要在目标机器中加载dll并与应用程序进行交互。

我探讨了使用JMI的可能性,但它增加了更多的复杂性。

如何使用JNA / JNI远程加载dll文件?

1 个答案:

答案 0 :(得分:0)

您可以相应地指定dll的位置。我正在开发一个需要类似功能的项目。请参阅以下代码。与客户端共享目标计算机中dll的位置后,可以通过指定路径来访问dll。

public class TestRemoteDll{
public native String readFile();


public static void main(String args[]){
    System.load("\\\\{Device's Name}\\Users\\Milan.AF\\Desktop\\RemoteDir\\Remotedll.dll");
    TestRemoteDll test = new TestRemoteDll();
    System.out.println("Calling native method!");
    String sum = test.readFile();
    System.out.println("Returned from Native Method");

    System.out.println(sum);
}

}

并确保你也相应地创建了dll(dll使用的文件也应该与客户端共享)。当你创建一个dll时,你必须以类似的方式指定文件的位置,如下所示。 / p>

#include "stdafx.h"
#include "iostream"
#include<string>
#include <fstream>
#include "Remotedll.h"
using namespace std;


string RemoteDll::readFile() {
int sum=0,x;
ifstream inFile;
inFile.open("\\\\{Device's Name}\\temp\\intSum.txt");
if (!inFile) {
    return "Failed to open file!";
        }
while (inFile >> x) {
    sum = sum + x;
}
inFile.close();
string str = to_string(sum);
return  "File operation successful! Sum =" + str    ;

}

我希望这会有所帮助。