bram
bram

Reputation: 1328

How to load dll library remotely?

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?

Upvotes: 1

Views: 728

Answers (1)

Milan
Milan

Reputation: 47

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.

Upvotes: 1

Related Questions