How to load dll library remotely?

馋奶兔 提交于 2019-12-12 06:08:14

问题


I have a remote machine which has an application installed and has its APIs written in C compiled into a dll.

I want to interact with the application using the APIs exposed by loading the dll through JNA in java remotely. i.e., my client code need to load the dll in the target machine and interact with the application.

I explored the possibility of using JMI, but it adds more complexity.

How to load dll files remotely using JNA/JNI?


回答1:


You can specify the location of the dll accordingly. I'm working on a project which requires similar features. Refer the code below . Once you shared the location of the dll in the target machine with your client , you can access the dll by specifying the path as shown below.

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);
}

}

And make sure you create the dll accordingly as well(The files dll use should be shared with the client as well ).You have to specify the location of files in a similar way when you create a dll as shown below.

#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    ;

}

I hope this helps.



来源:https://stackoverflow.com/questions/28595411/how-to-load-dll-library-remotely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!